1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
| public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List1.Items.Add("Финансы");
List1.Items.Add("Бухгалтерия");
List2.Items.Add("Экономика");
List2.Items.Add("Бизнес");
List2.Items.Add("Интернет");
List2.SelectionMode = SelectionMode.Multiple;
PrepareListBoxForDragDrop(List1);
PrepareListBoxForDragDrop(List2);
}
private void PrepareListBoxForDragDrop(ListBox listbox)
{
List1.AllowDrop = true;
listbox.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(ListboxPreviewMouseLeftButtonDown);
listbox.PreviewMouseMove += new MouseEventHandler(ListboxPreviewMouseMove);
listbox.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(ListboxPreviewMouseLeftButtonUp);
listbox.Drop += new DragEventHandler(ListboxDrop);
}
private static DependencyProperty DraggingElementProperty = DependencyProperty.RegisterAttached(
"DraggingElement", typeof(string), typeof(MainWindow), new FrameworkPropertyMetadata(null));
private static DependencyProperty IsDownProperty = DependencyProperty.RegisterAttached(
"IsDown", typeof(bool), typeof(MainWindow), new FrameworkPropertyMetadata(false));
private static DependencyProperty StartPointProperty = DependencyProperty.RegisterAttached(
"StartPoint", typeof(Point), typeof(MainWindow), new FrameworkPropertyMetadata(default(Point)));
private void ListboxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBox listbox = (ListBox)sender;
string s = GetBoundItemFromPoint(listbox, e.GetPosition(listbox));
if (s != null)
{
listbox.SetValue(IsDownProperty, true);
listbox.SetValue(DraggingElementProperty, s);
listbox.SetValue(StartPointProperty, e.GetPosition(listbox));
}
}
private string GetBoundItemFromPoint(ListBox box, Point point)
{
UIElement element = box.InputHitTest(point) as UIElement;
while (element != null)
{
if (element == box)
{
return null;
}
object item = box.ItemContainerGenerator.ItemFromContainer(element);
bool itemFound = !object.ReferenceEquals(item, DependencyProperty.UnsetValue);
if (itemFound)
{
return (string)item;
}
else
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
}
return null;
}
private void ListboxPreviewMouseMove(object sender, MouseEventArgs e)
{
ListBox listbox = (ListBox)sender;
bool isDown = (bool)listbox.GetValue(IsDownProperty);
if (!isDown)
{
return;
}
Point startPoint = (Point)listbox.GetValue(StartPointProperty);
if (Math.Abs(e.GetPosition(listbox).X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(e.GetPosition(listbox).Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
{
DragStarted(listbox);
}
}
private void DragStarted(ListBox List2)
{
List2.ClearValue(IsDownProperty);
System.Collections.ArrayList selected = new System.Collections.ArrayList(List2.SelectedItems);
foreach (Object selectedItem in selected)
{
string draggingElement = selectedItem.ToString();
DataObject d = new DataObject(DataFormats.UnicodeText, draggingElement);
DragDropEffects effects = DragDrop.DoDragDrop(List2, d, DragDropEffects.Copy | DragDropEffects.Move);
if ((effects & DragDropEffects.Move) != 0)
{
List1.ClearValue(IsDownProperty);
List2.Items.Remove(draggingElement);
}
}
}
private void ListboxDrop(object sender, DragEventArgs e)
{
ListBox List2 = (ListBox)sender;
if (e.Data.GetDataPresent(DataFormats.UnicodeText, true))
{
List2.Items.Add((string)e.Data.GetData(DataFormats.UnicodeText, true));
e.Effects =
((e.KeyStates & DragDropKeyStates.ControlKey) != 0) ?
DragDropEffects.Copy : DragDropEffects.Move;
e.Handled = true;
}
}
private void ListboxPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ListBox listbox = (ListBox)sender;
listbox.ClearValue(IsDownProperty);
listbox.ClearValue(DraggingElementProperty);
listbox.ClearValue(StartPointProperty);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
System.Collections.ArrayList selected = new System.Collections.ArrayList(List2.SelectedItems);
foreach (Object selectedItem in selected)
{
List2.Items.Remove(selectedItem);
List1.Items.Add(selectedItem);
}
}
} |