I.Introduction
Trong bày này mình xin chia sẻ về cách Drag And Drop Item in WPF :
II.Creating WPF Application
Fire Up Visual Studio 2010 và tạo WPF Application :
Tiếp theo là Design giao diện gồm 2 ListBox để kéo thả Item from ListBox One to ListBox Two :
Tiếp theo chúng ta tạo sự kiện cần thiết:
- Đối với ListBox One dùng sự kiện PreviewMouseLeftButtonDown …..
- Đối với ListBox Two dùng sự kiện Drop và đừng quên thiết lập thuộc tính AllowDrop=”True”
Sau đây là Code XAML giao diện :
1: <Window x:Class="DragAndDropListBoxSample.MainWindow"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Title="MainWindow" Height="600" Width="799">
5: <Grid>
6: <Grid.RowDefinitions>
7: <RowDefinition Height="50*" />
8: <RowDefinition Height="511*" />
9: </Grid.RowDefinitions>
10: <Grid.Background>
11: <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
12: <GradientStop Color="#FF7293FF" Offset="0" />
13: <GradientStop Color="White" Offset="0.993" />
14: </LinearGradientBrush>
15: </Grid.Background>
16: <StackPanel Height="50" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="778">
17: <StackPanel.Background>
18: <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
19: <GradientStop Color="Black" Offset="0" />
20: <GradientStop Color="White" Offset="1" />
21: </LinearGradientBrush>
22: </StackPanel.Background>
23: </StackPanel>
24: <ListBox PreviewMouseLeftButtonDown="lbxOne_PreviewMouseLeftButtonDown" Grid.Row="1" Height="372" HorizontalAlignment="Left" Margin="62,64,0,0" Name="lbxOne" VerticalAlignment="Top" Width="320" ScrollViewer.VerticalScrollBarVisibility="Visible" />
25: <ListBox Height="372" HorizontalAlignment="Left" Margin="404,64,0,0" Name="lbxTwo" VerticalAlignment="Top" Width="320" Grid.Row="1" AllowDrop="True" Drop="lbxTwo_Drop" ScrollViewer.VerticalScrollBarVisibility="Visible" />
26: <Label Content="ListBox One" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="62,30,0,0" Name="label1" VerticalAlignment="Top" Width="106" />
27: <Label Content="ListBox Two" Height="28" HorizontalAlignment="Left" Margin="404,30,0,0" Name="label2" VerticalAlignment="Top" Width="106" Grid.Row="1" />
28: </Grid>
29: </Window>
Tiếp theo chúng ta Xử lý sự kiện cho Ứng Dụng :
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Windows;
6: using System.Windows.Controls;
7: using System.Windows.Data;
8: using System.Windows.Documents;
9: 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;
14: using System.Collections.ObjectModel;
15: using System.Collections;
16:
17: namespace DragAndDropListBoxSample
18: {
19: /// <summary>
20: /// Drag And Drop in ListBox in WPF
21: /// </summary>
22: public partial class MainWindow : Window
23: {
24: /// <summary>
25: /// Load dữ liệu TimeZoneInfo đưa vào ListBox One
26: /// </summary>
27: ObservableCollection<string> zoneList = new ObservableCollection<string>();
28: ListBox DragSource;
29: public MainWindow()
30: {
31: InitializeComponent();
32: foreach (TimeZoneInfo time in TimeZoneInfo.GetSystemTimeZones())
33: {
34: zoneList.Add(time.ToString());
35: }
36: lbxOne.ItemsSource = zoneList;
37: }
38: /// <summary>
39: /// Xử Lý sự kiện khi Chọn Item đê Drag And Drop
40: /// </summary>
41: /// <param name="sender"></param>
42: /// <param name="e"></param>
43: private void lbxOne_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
44: {
45: ListBox Parent = (ListBox)sender;
46: DragSource = Parent;
47: object Data = GetDataFromListBox(DragSource,e.GetPosition(Parent));
48: if (Data != null)
49: DragDrop.DoDragDrop(Parent, Data, DragDropEffects.Move);
50: }
51: /// <summary>
52: /// Phương Thức Lấy dữ liệu từ ListBox
53: /// </summary>
54: /// <param name="Source"></param>
55: /// <param name="point"></param>
56: /// <returns></returns>
57: private static object GetDataFromListBox(ListBox Source, Point point)
58: {
59: UIElement element = Source.InputHitTest(point) as UIElement;
60: if (element != null)
61: {
62: object data = DependencyProperty.UnsetValue;
63: while (data == DependencyProperty.UnsetValue)
64: {
65: data = Source.ItemContainerGenerator.ItemFromContainer(element);
66: if (data == DependencyProperty.UnsetValue)
67: {
68: element = VisualTreeHelper.GetParent(element) as UIElement;
69: }
70: if (element == Source) return null;
71: if (data != DependencyProperty.UnsetValue)
72: {
73: return data;
74: }
75: }
76: }
77: return null;
78: }
79: /// <summary>
80: /// xử lý sự kiện Drop vào ListBox Two
81: /// </summary>
82: /// <param name="sender"></param>
83: /// <param name="e"></param>
84: private void lbxTwo_Drop(object sender, DragEventArgs e)
85: {
86: ListBox parent = (ListBox)sender;
87: object data = e.Data.GetData(typeof(string));
88: ((IList)DragSource.ItemsSource).Remove(data);
89: parent.Items.Add(data);
90: }
91: }
92: }
Run Application (Ctrl + F5) và Xem kết quả :