Using UWP TreeView and working on scenarios where I need to implement drop of one TreeView item to another, depending on it's properties (or type). For example, I have five nodes in TreeView, three of them are files, two are folders. File item can be dropped on Folder - but not vice versa. I can also drag File item from Folder into root but can not drop File item on another item that is also File. So you can see that there are multiple use-cases how TreeView items should behave. Was wondering that I can extend TreeView and then override DragEnter and DragLeave methods, maybe I could then detect underlaying object that is dragged and underlaying object that is dropped to ... but documentation is confusing, too general and lacking. Examples that I have all checked consider all items in TreeView equal (so I can drop Folder on File which is not permissible).
Here is my TreeView:
<TreeView
x:Name="treeview" Grid.Row="2" ItemsSource="{Binding storageFolders,Mode=OneWay}"
Style="{StaticResource TreeViewStyle1}"
>
<TreeView.ItemTemplate>
<DataTemplate x:DataType="localdata:FolderInfo">
<TreeViewItem ItemsSource="{x:Bind subFolders}" Content="{x:Bind FolderName}"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
And here is FolderInfo type:
public class FolderInfo : MyBase //INotifyPropertyChanged
{
private string _FolderName;
public string FolderName
{
get { return _FolderName; }
set
{
if (_FolderName != value)
{
_FolderName = value;
OnPropertyChanged("FolderName");
}
}
}
private bool _IsFolder;
public bool IsFolder
{
get { return _IsFolder; }
set
{
if (_IsFolder != value)
{
_IsFolder = value;
OnPropertyChanged("IsFolder");
}
}
}
public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();
public override string ToString()
{
return FolderName;
}
}
Storage folder is just an ObservableCollection in VM:
public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();