Posted on 2010-08-11 21:36
卞红军 阅读(616)
评论(0)
编辑
收藏
01 |
<Window x:Class= "WpfApplication1.MainWindow" |
04 |
Title= "MainWindow"
Height= "350"
Width= "525" > |
07 |
<Grid.ColumnDefinitions> |
08 |
<ColumnDefinition Width= "210" ></ColumnDefinition> |
09 |
<ColumnDefinition></ColumnDefinition> |
10 |
</Grid.ColumnDefinitions> |
12 |
<ListView x:Name= "listView1"
Grid.Column= "0"
AllowDrop= "True" |
13 |
PreviewMouseLeftButtonDown= "listView1_PreviewMouseLeftButtonDown" |
14 |
PreviewMouseMove= "listView1_PreviewMouseMove" > |
16 |
<Image Source= "Resources\Images\a01.jpg" ></Image> |
19 |
<Image Source= "Resources\Images\a02.jpg" ></Image> |
22 |
<Image Source= "Resources\Images\a03.jpg" ></Image> |
25 |
<Image Source= "Resources\Images\a04.jpg" ></Image> |
29 |
<Canvas x:Name= "canvas1"
Grid.Column= "1"
AllowDrop= "True" |
30 |
Drop= "canvas1_Drop"
DragEnter= "canvas1_DragEnter"
Background= "Red" > |
02 |
using System.Collections.Generic; |
06 |
using System.Windows.Controls; |
07 |
using System.Windows.Data; |
08 |
using System.Windows.Documents; |
09 |
using System.Windows.Input; |
10 |
using System.Windows.Media; |
11 |
using System.Windows.Media.Imaging; |
12 |
using System.Windows.Navigation; |
13 |
using System.Windows.Shapes; |
16 |
namespace WpfApplication1 |
19 |
/// Interaction logic for MainWindow.xaml |
21 |
public
partial class
MainWindow : Window |
28 |
InitializeComponent(); |
31 |
private
static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject |
39 |
current = VisualTreeHelper.GetParent(current); |
41 |
while
(current != null ); |
46 |
private
void listView1_PreviewMouseLeftButtonDown( object
sender, MouseButtonEventArgs e) |
48 |
startPoint = e.GetPosition( null ); |
51 |
private
void listView1_PreviewMouseMove( object
sender, MouseEventArgs e) |
53 |
Point mousPos = e.GetPosition( null ); |
54 |
Vector diff = startPoint - mousPos; |
56 |
if
((e.LeftButton == MouseButtonState.Pressed) && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance) && (Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)) |
58 |
ListViewItem listViewItem = FindAnchestor<ListViewItem>((DependencyObject)e.OriginalSource); |
59 |
if
(listViewItem == null ) {
return ; } |
61 |
ListView listView = sender
as ListView; |
62 |
contact = (ListViewItem)listView.ItemContainerGenerator.ItemFromContainer(listViewItem); |
63 |
DataObject dataObject =
new DataObject( "MyFormat" , contact); |
64 |
DragDrop.DoDragDrop(listViewItem, dataObject, DragDropEffects.Move); |
68 |
private
void canvas1_Drop( object
sender, DragEventArgs e) |
70 |
if
(e.Data.GetDataPresent( "MyFormat" )) |
72 |
contact = e.Data.GetData( "MyFormat" )
as ListBoxItem; |
73 |
Canvas CanvasView = sender
as Canvas; |
74 |
this .listView1.Items.Remove(contact); |
75 |
CanvasView.Children.Add(contact); |
79 |
private
void canvas1_DragEnter( object
sender, DragEventArgs e) |
81 |
if
(!(e.Data.GetDataPresent( "contact" )) || (sender == e.Source)) |
83 |
e.Effects = DragDropEffects.None; |
参考阅读:
Drag and Drop in WPF
http://www.wpftutorial.net/DragAndDrop.html