Hi Rolf,
Please see following blog for detailed steps and code to drag & drop any
UIElement:
#Marcelo's WebLog : Dragging WPF elements
http://blogs.msdn.com/marcelolr/archive/2006/03/02/542641.aspx
Here's the C# equivalent code of the sample:
<Window x:Class="WpfApplication2.Window1"
xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
<Border DockPanel.Dock="Top" BorderBrush="DarkGray"
BorderThickness="2" Padding="4">
<TextBlock FontSize="8pt" FontFamily="Tahoma"
TextWrapping="Wrap">
<Bold>Drag Canvas Sample
</Bold><LineBreak /><Run>
This sample demonstrates the easiest way to drag shapes (or any other
elements) on a Canvas area.</Run>
</TextBlock>
</Border>
<Canvas Name="MyCanvas">
<Canvas.Background>
<LinearGradientBrush>
<LinearGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="DarkBlue" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Canvas.Background>
<Rectangle Canvas.Top="8" Canvas.Left="8"
Width="32" Height="32" Fill="LightPink" />
<Ellipse Canvas.Top="8" Canvas.Left="48"
Width="40" Height="16" Fill="Tan" />
<Rectangle Canvas.Top="48" Canvas.Left="8"
Width="32" Height="62" Fill="Green" />
<Ellipse Canvas.Top="48" Canvas.Left="48"
Width="62" Height="62" Fill="Orange" />
</Canvas>
</DockPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private Point m_StartPoint; // where did the mouse start off from?
private Double m_OriginalLeft; // what was the element's original x
offset?
private Double m_OriginalTop; // what was the element's original
y offset?
private Boolean m_IsDown; // Is the mouse down right now?
private Boolean m_IsDragging; // Are we actually dragging the shape
around?
private UIElement m_OriginalElement; // What is it that we're
dragging?
private Rectangle m_OverlayElement; // What is it that we're using
to show where the shape will end up?
public Window1()
{
InitializeComponent();
MyCanvas.PreviewMouseLeftButtonDown += new
MouseButtonEventHandler(MyCanvas_PreviewMouseLeftButtonDown);
MyCanvas.PreviewMouseMove += new
MouseEventHandler(MyCanvas_PreviewMouseMove);
MyCanvas.PreviewMouseLeftButtonUp += new
MouseButtonEventHandler(MyCanvas_PreviewMouseLeftButtonUp);
this.PreviewKeyDown += new
KeyEventHandler(Window1_PreviewKeyDown);
}
void Window1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Escape && m_IsDragging)
{
DragFinished(true);
}
}
void MyCanvas_PreviewMouseLeftButtonUp(object sender,
MouseButtonEventArgs e)
{
if (m_IsDown)
{
DragFinished(false);
e.Handled = true;
}
}
private void DragFinished(Boolean cancelled)
{
System.Windows.Input.Mouse.Capture(null);
if (m_IsDragging)
{
MyCanvas.Children.Remove(m_OverlayElement);
if (!cancelled)
{
Canvas.SetLeft(m_OriginalElement,
Canvas.GetLeft(m_OverlayElement));
Canvas.SetTop(m_OriginalElement,
Canvas.GetTop(m_OverlayElement));
}
m_OverlayElement = null;
}
m_IsDragging = false;
m_IsDown = false;
}
void MyCanvas_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (m_IsDown)
{
if (!m_IsDragging && Math.Abs(e.GetPosition(MyCanvas).X -
m_StartPoint.X) > SystemParameters.MinimumHorizontalDragDistance &&
Math.Abs(e.GetPosition(MyCanvas).Y - m_StartPoint.Y) >
SystemParameters.MinimumHorizontalDragDistance)
{
DragStarted();
}
if (m_IsDragging)
{
DragMoved();
}
}
}
private void DragStarted()
{
m_IsDragging = true;
m_OriginalLeft = Canvas.GetLeft(m_OriginalElement);
m_OriginalTop = Canvas.GetTop(m_OriginalElement);
// Add a rectangle to indicate where we'll end up
// We'll just use an alpha-blended visual brush
VisualBrush brush = new VisualBrush(m_OriginalElement);
brush.Opacity = 0.5;
m_OverlayElement = new Rectangle();
m_OverlayElement.Width = m_OriginalElement.RenderSize.Width;
m_OverlayElement.Height = m_OriginalElement.RenderSize.Height;
m_OverlayElement.Fill = brush;
MyCanvas.Children.Add(m_OverlayElement);
}
private void DragMoved()
{
Point currentPosition =
System.Windows.Input.Mouse.GetPosition(MyCanvas);
Double elementLeft = (currentPosition.X - m_StartPoint.X) +
m_OriginalLeft;
Double elementTop = (currentPosition.Y - m_StartPoint.Y) +
m_OriginalTop;
Canvas.SetLeft(m_OverlayElement, elementLeft);
Canvas.SetTop(m_OverlayElement, elementTop);
}
void MyCanvas_PreviewMouseLeftButtonDown(object sender,
MouseButtonEventArgs e)
{
if (MyCanvas == e.Source) return;
m_IsDown = true;
m_StartPoint = e.GetPosition(MyCanvas);
m_OriginalElement = (UIElement) e.Source;
MyCanvas.CaptureMouse();
e.Handled = true;
}
}
}
Regards,
Walter Wang (
[email protected], remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.