Pro Silverlight 3 in C# 拖拽示例

<UserControl
    xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    x:Class="HichinaMeetingShow.UI.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="parentCanvas" MouseLeftButtonDown="canvas_Click" Background="White">
    </Canvas>
</UserControl>

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 HichinaMeetingShow.UI
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        // Keep track of when an ellipse is being dragged.
        private bool isDragging = false;
        // When an ellipse is clicked, record the exact position
        // where the click is made.
        private Point mouseOffset;
        private void canvas_Click(object sender, MouseButtonEventArgs e)
        {
            // Create an ellipse (unless the user is in the process
            // of dragging another one).
            if (!isDragging)
            {
                // Give the ellipse a 50-pixel diameter and a red fill.
                Ellipse ellipse = new Ellipse();
                ellipse.Fill = new SolidColorBrush(Colors.Red);
                ellipse.Width = 50;
                ellipse.Height = 50;
                // Use the current mouse position for the center of
                // the ellipse.
                Point point = e.GetPosition(this);
                ellipse.SetValue(Canvas.TopProperty, point.Y - ellipse.Height / 2);
                ellipse.SetValue(Canvas.LeftProperty, point.X - ellipse.Width / 2);
                // Watch for left-button clicks.
                ellipse.MouseLeftButtonDown += ellipse_MouseDown;
                // Add the ellipse to the Canvas.
                parentCanvas.Children.Add(ellipse);
            }
        }

        private void ellipse_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // Dragging mode begins.
            isDragging = true;
            Ellipse ellipse = (Ellipse)sender;
            // Get the position of the click relative to the ellipse
            // so the top-left corner of the ellipse is (0,0).
            mouseOffset = e.GetPosition(ellipse);
            // Change the ellipse color.
            ellipse.Fill = new SolidColorBrush(Colors.Green);
            // Watch this ellipse for more mouse events.
            ellipse.MouseMove += ellipse_MouseMove;
            ellipse.MouseLeftButtonUp += ellipse_MouseUp;
            // Capture the mouse. This way you'll keep receiving
            // the MouseMove event even if the user jerks the mouse
            // off the ellipse.
            ellipse.CaptureMouse();
        }

        private void ellipse_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Ellipse ellipse = (Ellipse)sender;
                // Get the position of the ellipse relative to the Canvas.
                Point point = e.GetPosition(parentCanvas);
                // Move the ellipse.
                ellipse.SetValue(Canvas.TopProperty, point.Y - mouseOffset.Y);
                ellipse.SetValue(Canvas.LeftProperty, point.X - mouseOffset.X);
            }
        }

        private void ellipse_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                Ellipse ellipse = (Ellipse)sender;
                // Change the ellipse color.
                ellipse.Fill = new SolidColorBrush(Colors.Orange);
                // Don't watch the mouse events any longer.
                ellipse.MouseMove -= ellipse_MouseMove;
                ellipse.MouseLeftButtonUp -= ellipse_MouseUp;
                ellipse.ReleaseMouseCapture();
                isDragging = false;
            }
        }
    }
}

book mark: 182

原文地址:https://www.cnblogs.com/zhanxp/p/1716611.html