xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF " Height="300" Width="300">
A
B
C
D
E
F
G
H
DragEnter="cvsSurface_DragEnter" Drop="cvsSurface_Drop"
Name="cvsSurface" >
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class Window1 : Window
{
private ListBoxItem draggedItem;
private Point startDragPoint;
public Window1()
{
InitializeComponent();
}
private void cvsSurface_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void cvsSurface_Drop(object sender, DragEventArgs e)
{
Label newLabel = new Label();
newLabel.Content = e.Data.GetData(DataFormats.Text);
cvsSurface.Children.Add(newLabel);
Canvas.SetLeft(newLabel,100);
Canvas.SetTop(newLabel, 200);
}
private void ListBoxItem_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
draggedItem = sender as ListBoxItem;
startDragPoint = e.GetPosition(null);
}
private void ListBoxItem_PreviewMouseMove(object sender,MouseEventArgs e)
{
Point position = e.GetPosition(null);
DragDrop.DoDragDrop(draggedItem, draggedItem.Content,DragDropEffects.Copy);
}
}
}