<UserControl x:Class="RiaDragAndDrop.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas Width="400" Height="300">
<TextBlock x:Name="txtDrag" Text="드래그앤드롭" FontSize="30"></TextBlock>
</Canvas>
</UserControl>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace RiaDragAndDrop
{
public partial class Page : UserControl
{
private bool isMouseDown = false;
private Point lastPoint = new Point();
private Point offset = new Point();
public Page()
{
InitializeComponent();
txtDrag.MouseLeftButtonDown += new MouseButtonEventHandler(txtDrag_MouseLeftButtonDown);
txtDrag.MouseMove += new MouseEventHandler(txtDrag_MouseMove);
txtDrag.MouseLeftButtonUp += new MouseButtonEventHandler(txtDrag_MouseLeftButtonUp);
}
void txtDrag_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
txtDrag.ReleaseMouseCapture();
isMouseDown = false;
}
void txtDrag_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
lastPoint = e.GetPosition(null);
txtDrag.SetValue(Canvas.LeftProperty, (lastPoint.X - offset.X));
txtDrag.SetValue(Canvas.TopProperty, (lastPoint.Y - offset.Y));
}
}
void txtDrag_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
txtDrag.CaptureMouse();
isMouseDown = true;
lastPoint = e.GetPosition(null);
offset.X = lastPoint.X - Convert.ToDouble(txtDrag.GetValue(Canvas.LeftProperty));
offset.Y = lastPoint.Y - Convert.ToDouble(txtDrag.GetValue(Canvas.TopProperty));
}
}
}