The mouse-related event handlers for implementing drag-and-drop : Mouse « Events « Silverlight






The mouse-related event handlers for implementing drag-and-drop

The mouse-related event handlers for implementing drag-and-drop
    

<UserControl x:Class='SilverlightApplication3.MainPage'
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' d:DesignHeight='480'>
    <Canvas x:Name="LayoutRoot" Background="Black">
        <TextBlock x:Name="myTextBlock" Foreground="White" Text="Waiting..." MouseLeftButtonDown="MyTextBlock_MouseLeftButtonDown" MouseMove="MyTextBlock_MouseMove" MouseLeftButtonUp="MyTextBlock_MouseLeftButtonUp" /> 
    </Canvas>
</UserControl>

//File: Page.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        private bool isMouseDown = false;
        private Point lastPoint = new Point();
        private Point offset = new Point();

        public MainPage()
        {
            InitializeComponent();
        }

        public void MyTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)                                               
        {                                                                         
            myTextBlock.CaptureMouse();                                             
            isMouseDown = true;                                                     
                                                                                
            lastPoint = e.GetPosition(null);                                        
            offset.X = lastPoint.X - Convert.ToDouble(myTextBlock.GetValue(Canvas.LeftProperty));
            offset.Y = lastPoint.Y - Convert.ToDouble(myTextBlock.GetValue(Canvas.TopProperty));          
        }                                                                         

        public void MyTextBlock_MouseMove(object sender, MouseEventArgs e)        
        {                                                                         
            if (isMouseDown == true)                                                
            {                                                                       
                lastPoint = e.GetPosition(null);                                      
                myTextBlock.SetValue(Canvas.LeftProperty, (lastPoint.X-offset.X));    
                myTextBlock.SetValue(Canvas.TopProperty, (lastPoint.Y-offset.Y));     
            }                                                                       
        }                                                                         

        public void MyTextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)                                                 
        {                                                                         
            myTextBlock.ReleaseMouseCapture();                                      
            isMouseDown = false;                                                   
        }   
    }
}

   
    
    
    
  








Related examples in the same category

1.Grid MouseMoveGrid MouseMove
2.Grid MouseLeftButtonDownGrid MouseLeftButtonDown
3.Grid MouseLeftButtonUpGrid MouseLeftButtonUp
4.Attaches a MouseEnter Event Handler to a TextBlock Silverlight ControlAttaches a MouseEnter Event Handler to a TextBlock Silverlight Control
5.Use Mouse events to control animationUse Mouse events to control animation
6.Mouse down, up and moved eventsMouse down, up and moved events