Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

MouseDragElementBehavior: accessing coordinates RSS

2 replies

Last post Aug 15, 2009 02:24 PM by Pravinkumar R. D.

(0)
  • a.pierini

    a.pierini

    Participant

    1015 Points

    337 Posts

    MouseDragElementBehavior: accessing coordinates

    Aug 14, 2009 12:36 PM | LINK

    Hi,

    in this piece of code 

     

    <Rectangle x:Name="rctDrawingArea" Fill="#55FFFFFF" Stroke="Black">

    <i:Interaction.Behaviors>

    <il:MouseDragElementBehavior x:Name="mdebDrawingArea"  ConstrainToParentBounds="True"/>

    </i:Interaction.Behaviors>

    </Rectangle>

     is there a way to access mdebDrawingArea.X (and Y)?

    I mean my rectangle is inside a grid and once I dragged it around  I need to know its final offset. I thought to get access to X and Y property because I noticed Margin property of the rectangle is not updated.

     Thank you all!

    MouseDragElementBehavior

    If the reply helps you, please kindly mark the post as answer.
    Follow me on Twitter:@parseval


    www.rosemakeup.com
  • DJanjicek

    DJanjicek

    Participant

    1575 Points

    487 Posts

    Re: MouseDragElementBehavior: accessing coordinates

    Aug 14, 2009 10:09 PM | LINK

    I'm not aware of your code, but if it's something like this http://www.rick-sammons.com/?tag=/mousedragelementbehavior you can use the eventhandler to receive the x/y coordinates which are exposed by MouseEventArgs

    void Bla(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        MessageBox.Show("X: " + e.X.ToString() + " " + "Y: " + e.Y.ToString());
    }

  • Pravinkumar R. D.

    Pravinkumar...

    Contributor

    5034 Points

    801 Posts

    Re: MouseDragElementBehavior: accessing coordinates

    Aug 15, 2009 02:24 PM | LINK

    Hi,

    Here is a code where you can track what is the position of X and Y co-ordinates-

    1) I have applied the behavior by code. The class MouseDragElementBehavior contains few events. One of them is DragFinished. You can get the co-ordinates within that event as well. Look at the code below-

    USER Control-

    <UserControl

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

    xmlns:il="clr-namespace:Microsoft.Expression.Interactivity.Layout;assembly=Microsoft.Expression.Interactions"

    x:Class="SampleProject.MainPage"

    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">

    <TextBlock x:Name="txtcordinates" FontSize="20" Foreground="Blue"/>

    <Rectangle Fill="Red" Stroke="Black" Margin="144,88,160,192" x:Name="rect1"/>

    </Grid>

    </UserControl>

    UserControl Code in CS file-

    Import a namespace in .CS file codebehind of MainPage.XAML

    using Microsoft.Expression.Interactivity.Layout;

    public partial class MainPage : UserControl
    	{
    		public MainPage()
    		{
    			// Required to initialize variables
    			InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    		}
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                MouseDragElementBehavior behavior = new MouseDragElementBehavior();
                behavior.Attach(rect1);
                behavior.DragFinished += new MouseEventHandler(behavior_DragFinished);
             
            }
    
            void behavior_DragFinished(object sender, MouseEventArgs e)
            {
                Point getposition = e.GetPosition(rect1);
                txtcordinates.Text="Position X : " + getposition.X + "                " + "Position Y" + getposition.Y;
            }
    
           
    }
    Let me know If this works for you!!
    Thanks,
    Pravin
    "Please Mark as answered, If this answers your question"

    Silverlight 3.0