WPF 实现拖放的简单实例(Drag & Drop)

概述:拖放的原理不再说明,wpf实现的简单实例

一、XAML部分

XAML
 1 <Window x:Class="WpfDragDropGrid.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Loaded="Window_Loaded"
 5         Title="MainWindow" Height="450" Width="600">
 6     <Grid>
 7         <Grid.ColumnDefinitions>
 8             <ColumnDefinition Width="269*"/>
 9             <ColumnDefinition Width="11*"/>
10             <ColumnDefinition Width="312*"/>
11         </Grid.ColumnDefinitions>
12         <DataGrid x:Name="dgRight" Margin="0" Grid.Column="2" AutoGenerateColumns="False" Drop="dgRight_Drop"
13                   IsReadOnly="True" SelectionMode="Single" AllowDrop="True">
14             <DataGrid.Columns>
15                 <DataGridTextColumn Binding="{Binding devname}" ClipboardContentBinding="{x:Null}" Header="设备" Width="*"/>
16                 <DataGridTextColumn Binding="{Binding spot1}" ClipboardContentBinding="{x:Null}" Header="测点1" Width="*"/>
17                 <DataGridTextColumn Binding="{Binding spot2}" ClipboardContentBinding="{x:Null}" Header="测点2" Width="*"/>
18                 <DataGridTextColumn Binding="{Binding spot3}" ClipboardContentBinding="{x:Null}" Header="测点3" Width="*"/>
19             </DataGrid.Columns>
20         </DataGrid>
21         <ListBox x:Name="listBox" Margin="0" Background="#FFF1EFBD" AllowDrop="True"
22                  PreviewMouseLeftButtonDown="listBox_PreviewMouseLeftButtonDown"/>
23     </Grid>
24 </Window>

二、代码部分

后台代码
 1 using System;
 2 using System.Collections.ObjectModel;
 3 using System.Windows;
 4 using System.Windows.Controls;
 5 using System.Windows.Input;
 6 using System.Windows.Media;
 7 
 8 namespace WpfDragDropGrid
 9 {
10     /// <summary>
11     /// MainWindow.xaml 的交互逻辑
12     /// </summary>
13     public partial class MainWindow : Window
14     {
15         ObservableCollection<Record> recordLeftList = new ObservableCollection<Record>();
16         ObservableCollection<Record> recordRightList = new ObservableCollection<Record>();
17         public MainWindow()
18         {
19             InitializeComponent();
20         }
21 
22         private void Window_Loaded(object sender, RoutedEventArgs e)
23         {
24             listBox.ItemsSource = recordLeftList;
25             listBox.DisplayMemberPath = "devname";
26             dgRight.ItemsSource = recordRightList;
27             Random ran = new Random();
28             for (int i = 0; i < 10; i++)
29             {
30                 recordLeftList.Add(new Record() { devname="dev"+i,spot1=ran.Next(1,100),spot2=ran.Next(1,100),spot3=ran.Next(1,100)});
31             }
32         }
33 
34         private void listBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
35         {
36             Point pos = e.GetPosition(listBox);
37             HitTestResult result = VisualTreeHelper.HitTest(listBox, pos);
38             if (null == result) return;
39 
40             ListBoxItem listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit); // Find your actual visual you want to drag
41             if (listBoxItem == null || listBoxItem.Content != listBox.SelectedItem || !(listBox.SelectedItem is Record))
42                 return;
43 
44             Record record = listBoxItem.Content as Record;
45             DataObject dataObject = new DataObject(record);
46             // Here, we should notice that dragsource param will specify on which 
47             // control the drag&drop event will be fired
48             System.Windows.DragDrop.DoDragDrop(listBox, dataObject, DragDropEffects.Copy);
49             
50         }
51 
52         private void dgRight_Drop(object sender, DragEventArgs e)
53         {
54             Point pos = e.GetPosition(dgRight);
55             HitTestResult result = VisualTreeHelper.HitTest(dgRight, pos);
56             if (result == null)
57                 return;
58 
59             Record sourceRecord = e.Data.GetData(typeof(Record)) as Record;
60             if (sourceRecord != null)
61                 recordRightList.Add(sourceRecord);
62         }
63     }
64 
65     //工具类方法,来自网络(很通用,所以借鉴于此,多谢原创)
66     internal static class Utils
67     {
68         //根据子元素查找父元素
69         public static T FindVisualParent<T>(DependencyObject obj) where T : class
70         {
71             while (obj != null)
72             {
73                 if (obj is T)
74                     return obj as T;
75 
76                 obj = VisualTreeHelper.GetParent(obj);
77             }
78             return null;
79         }
80     }
81 
82     //实体类
83     public class Record
84     {
85         public string devname
86         { get; set; }
87 
88         public double spot1
89         { get; set; }
90 
91         public double spot2
92         { get; set; }
93 
94         public double spot3
95         { get; set;  }
96     }
97 }

 

posted @ 2012-08-28 15:40  liancs  阅读(1060)  评论(0编辑  收藏  举报