microsoft-ui-xaml: Question: How to block a drop using the DragOver event in TreeView or TreeViewItem?
I have a TreeView that contains two types of tree items, Leaf and Group.
Leaves can be children of groups. Groups can be children of groups. Leaves cannot have children.
Within the same tree, I want to allow my customers to reorder the tree and to move items to different groups by dragging and dropping elements. Based on the rules provided above, I need to be able to conditionally block certain drop operations, and I want to block them during the DragOver event. I have tried the following code, but it does nothing to block the drop:
<c:TreeView ItemsSource="{x:Bind ViewModel.Nodes}"
DragOver="TreeView_DragOver"
DragEnter="TreeView_DragEnter"
DragItemsCompleted="TreeView_DragItemsCompleted"
DragItemsStarting="TreeView_DragItemsStarting">
<c:TreeView.ItemTemplate>
<DataTemplate x:DataType="local:ExplorerItem">
<local:MyTreeViewItem ItemsSource="{x:Bind Children}" Content="{x:Bind Name}" AllowDrop="True" DragOver="TreeViewItem_DragOver"
DragEnter="TreeViewItem_DragEnter"
Drop="TreeViewItem_Drop" DropCompleted="TreeViewItem_DropCompleted"/>
</DataTemplate>
</c:TreeView.ItemTemplate>
</c:TreeView>
private void TreeViewItem_DragOver(object sender, DragEventArgs e)
{
// this does not do anything
e.AcceptedOperation = DataPackageOperation.None;
}
private void TreeView_DragOver(object sender, DragEventArgs e)
{
// this does not do anything
e.AcceptedOperation = DataPackageOperation.None;
}
private void TreeViewItem_DragEnter(object sender, DragEventArgs e)
{
// this does not do anything
e.AcceptedOperation = DataPackageOperation.None;
}
private void TreeView_DragEnter(object sender, DragEventArgs e)
{
// this does not do anything
e.AcceptedOperation = DataPackageOperation.None;
}
Am I doing something wrong? Can someone show me how to conditionally block a drop operation?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 45 (27 by maintainers)
@kaiguo that would be awesome.
I think I have the same issue (in #381) as @dpaulino, I want to be able to enforce the order of items (I.e. have pinned items at the top of the tree view) and block moving certain items and make certain items not be drop targets.
Thanks!
@lukasf thanks for referencing that proposal. I would like to see this feature added ASAP though rather than wait for WinUI 3.0. My customers have been demanding proper drag and drop abilities for the past 4 months, and I cannot implement it properly due to this treeview limitation.
WinUI team, please consider addressing this as soon as possible.
There is already a proposal for this: #381
I hope it will be considered, because it is a very common scenario that you want to prevent specific drag/drop operations, depending on source and target nodes.
Weird. I seem to have fixed it by cleaning the solution and then rebuilding.
The observable collection is being updated now. Going to do a few more tests before I close this issue.