I am trying to understand Drag and Drop login in WPF application. I have made simple example and I think I got ListBox items already, but I can't understand how to Drop selected item from ListBox into TreeView. Can somebody provide the solution to this problem so I can understand the logic? I have went through many examples, but couldn't get it working in my model. What is currently wrong?
MainWindow.xaml.cs:
using DragnDrop.Models;
using System;
using System.Collections;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace DragnDrop
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
ListBox dragSource = null;
private void TreeView_Drop(object sender, DragEventArgs e)
{
TreeView parent = (TreeView)sender;
object data = e.Data.GetData(typeof(string));
((IList)dragSource.ItemsSource).Remove(data);
//parent.Items.Add(data);
Debug.WriteLine(data);
}
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
dragSource = parent;
object data = GetDataFromListBox(dragSource, e.GetPosition(parent));
if (data != null)
{
DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
}
}
private static object GetDataFromListBox(ListBox source, Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if (element != null)
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
if (element == source)
{
return null;
}
}
if (data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
}
}
MainWindow.xaml:
<Window x:Class="DragnDrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:DragnDrop"
xmlns:model="clr-namespace:DragnDrop.Models"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<self:MainViewModel />
</Window.DataContext>
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="Category structure" FontSize="18" Margin="0,0,0,10"/>
<TreeView Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding TreeViewItems}"
FontSize="14"
AllowDrop="True"
Drop="TreeView_Drop">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:CategoryItem}" ItemsSource="{Binding Path=CategoryItems}">
<TextBlock Text="{Binding Path=ItemName}" Margin="3,3,3,3"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<TextBlock Grid.Column="1" Grid.Row="0" Text="Applications" FontSize="18" Margin="0,0,0,10"/>
<ListBox Grid.Column="1" Grid.Row="1" Name="lbTodoList"
HorizontalContentAlignment="Stretch"
FontSize="14"
ItemsSource="{Binding ListBoxItems}"
PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
Here is link to repo with working example: https://github.com/vadimffe/DragnDrop

