using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GongSolutions.Wpf.DragDrop;
using GongSolutions.Wpf.DragDrop.Utilities;
using MahApps.Metro.Controls;
namespace CaptainRecommendUI
public partial class MainWindow : MetroWindow
public static MainWindow Instance;
for (int i = 0; i < 3; ++i)
TreeViewItem root = new TreeViewItem() {Header = "Root" + i};
root.Items.Add(new TreeViewItem() {Header = "Son1"});
root.Items.Add(new TreeViewItem() {Header = "Son2"});
root.Items.Add(new TreeViewItem() {Header = "Son3"});
AllowMultiSelection(trvMenu);
trvMenu.DataContext = new TreeViewDrop();
TextBlock1.DataContext = new TreeViewDrop();
private static readonly PropertyInfo IsSelectionChangeActiveProperty = typeof (TreeView).GetProperty
"IsSelectionChangeActive",
BindingFlags.NonPublic | BindingFlags.Instance
public static void AllowMultiSelection(TreeView treeView)
if (IsSelectionChangeActiveProperty == null) return;
var selectedItems = new List<TreeViewItem>();
treeView.SelectedItemChanged += (a, b) =>
var treeViewItem = treeView.SelectedItem as TreeViewItem;
if (treeViewItem == null) return;
var isSelectionChangeActive = IsSelectionChangeActiveProperty.GetValue(treeView, null);
IsSelectionChangeActiveProperty.SetValue(treeView, true, null);
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl) ||
Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
selectedItems.ForEach(item => item.IsSelected = true);
selectedItems.ForEach(item => item.IsSelected = (item == treeViewItem));
if (!selectedItems.Contains(treeViewItem))
selectedItems.Add(treeViewItem);
treeViewItem.IsSelected = false;
selectedItems.Remove(treeViewItem);
IsSelectionChangeActiveProperty.SetValue(treeView, isSelectionChangeActive, null);
public class TreeViewDrop : IDropTarget
public void DragOver(IDropInfo dropInfo)
Debug.WriteLine("DRAGOVER ");
dropInfo.Effects = DragDropEffects.Move;
public void Drop(IDropInfo dropInfo)
MainWindow.Instance.TextBlock1.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
Debug.WriteLine("Drop ");
x:Class="CaptainRecommendUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:dragDrop="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
xmlns:captainRecommendUi="clr-namespace:CaptainRecommendUI"
Title="MainWindow" Height="600" Width="700"
BorderBrush="{DynamicResource AccentColorBrush}"
WindowStartupLocation="CenterScreen">
<ColumnDefinition Width="200" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TreeView Name="trvMenu" AllowDrop="True"
dragDrop:DragDrop.IsDragSource="True">
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Title}" />
</HierarchicalDataTemplate>
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
<TextBlock Grid.Row="0" Name="TextBlock1"
Text="Hello! I am a Target." Foreground="Red"
dragDrop:DragDrop.IsDropTarget="True"
dragDrop:DragDrop.DropHandler="{Binding}">
Visibility="{Binding IsEmpty,Converter={StaticResource BooleanToVisibilityConverter}}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
<TextBlock FontSize="100" HorizontalAlignment="Center" Margin="0 0 0 4" Text="}"/>
<TextBlock FontSize="26" HorizontalAlignment="Center" Margin="0 0 0 4" Text="Drag items here"/>
<!--<views:FileIcon BorderThickness="0" Width="50" Height="50" Foreground="{DynamicResource PrimaryHueMidBrush}"/>-->
<TextBlock FontSize="26" Margin="0 4 0 0" Text="to get recommendations" HorizontalAlignment="Center"/>