Move the element from one ListBox'a to another.
-
There are 4 ListBox, of which three can be reset and tagged into the fourth, not moved but copied. How do you describe the events of the host ListBox?
<ListBox x:Name="listhelmets" Height="214" Width="248" ItemsSource="{Binding ListHelmets}" IsSynchronizedWithCurrentItem="True" Canvas.Left="464"Canvas.Top="37" PreviewMouseDown="helmet_MouseDown1" PreviewMouseLeftButtonDown="helmet_PreviewMouseLeftButtonDown" DragLeave="helmet_DragLeave" PreviewMouseMove="helmet_PreviewMouseMove" SelectedValuePath="protection"> <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal"> <Image Source="{Binding Path=Image}" Width="56" Height="61"/> <TextBox Height="30" Width="30"> <Binding Path="protection" /> </TextBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
in this:
<ListBox x:Name="listHero" Height="148" Width="158" <ListBox.ItemTemplate > <DataTemplate > <StackPanel Orientation="Horizontal"> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
for the first ListBox:
private void helmet_MouseDown1(object sender, MouseButtonEventArgs e) { _startPoint = e.GetPosition(null);
} private void helmet_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed) return;
Point mousePos = e.GetPosition(null);
Vector diff = startPoint - mousePos;
if (Math.Abs(diff.X) <= SystemParameters.MinimumHorizontalDragDistance
&& Math.Abs(diff.Y) <= SystemParameters.MinimumVerticalDragDistance)
return;
var lst = sender as ListBox;
var li = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);
Console.WriteLine("move " + li);
if (li == null) return;
var str = lst.ItemContainerGenerator.ItemFromContainer(li);
var data = new DataObject("txt", str);
var res = DragDrop.DoDragDrop(li, data, DragDropEffects.All);
if (res == DragDropEffects.Move)
(lst.ItemsSource as IList).Remove(str);
}
static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject
{
do
{
if (current is T) return (T)current;
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
UPDATE private void listHero_Drop(object sender, System.Windows.DragEventArgs e)
{
var o = e.Data.GetData("txt");
var lst = sender as ListBox;
(lst.ItemsSource as IList).Add(o);
e.Effects = DragDropEffects.Move;
}private void ListHero_OnDragEnter(object sender, System.Windows.DragEventArgs e) { if (e.Data.GetDataPresent("txt")) e.Effects = DragDropEffects.Move;
}`
-
How do you describe the events of the receiving leafbox?
Add Drop and DragEnter
<ListBox Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" />
In cs add a code for processors, approximately:
void ListBox_Drop(object sender, DragEventArgs e) { var o = e.Data.GetData(...) as ...; ... e.Effects = DragDropEffects....; } void ListBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(...)) e.Effects = DragDropEffects....; }
The code to be in processors is in the example. https://ru.stackoverflow.com/revisions/477112/3 ♪
UPDATE: https://ru.stackoverflow.com/revisions/478351/3 , stacking the lines, replaced the following.
UPDATE: v2, retrieval of objects of different type.<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300" Height="300"> <StackPanel> <ListBox ItemsSource="{Binding Items1}" Height="100" PreviewMouseLeftButtonDown="DragList_PreviewMouseLeftButtonDown" PreviewMouseMove="DragList_PreviewMouseMove"/> <ListBox ItemsSource="{Binding Items2}" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" Background="LightYellow" Height="200" AllowDrop="True" /> </StackPanel> </Window>
interface IData { } class Data1 : IData { public override string ToString() { return "Data1"; }} class Data2 : IData { public override string ToString() { return "Data2"; }}
partial class MainWindow : Window {
public MainWindow() { Items1 = new ObservableCollection<object>() { new Data1(), new Data2() }; Items2 = new ObservableCollection<object>(); this.DataContext = this; } public ObservableCollection<object> Items1 { get; set; } public ObservableCollection<object> Items2 { get; set; } Point startPoint; void DragList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); } void DragList_PreviewMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton != MouseButtonState.Pressed) return; Point mousePos = e.GetPosition(null); Vector diff = startPoint - mousePos; if (Math.Abs(diff.X) <= SystemParameters.MinimumHorizontalDragDistance && Math.Abs(diff.Y) <= SystemParameters.MinimumVerticalDragDistance) return; var lst = sender as ListBox; var li = FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource); Console.WriteLine("move " + li); if (li == null) return; var o = lst.ItemContainerGenerator.ItemFromContainer(li); var data = new DataObject(typeof(IData), o); var res = DragDrop.DoDragDrop(li, data, DragDropEffects.All); if (res == DragDropEffects.Move) (lst.ItemsSource as IList).Remove(o); } void ListBox_Drop(object sender, DragEventArgs e) { var o = e.Data.GetData(typeof(IData)); var lst = sender as ListBox; (lst.ItemsSource as IList).Add(o); e.Effects = DragDropEffects.Move; } void ListBox_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(IData))) e.Effects = DragDropEffects.Move; } static T FindAnchestor<T>(DependencyObject current) where T : DependencyObject { do { if (current is T) return (T)current; current = VisualTreeHelper.GetParent(current); } while (current != null); return null; }
}