Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

  • 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