microsoft / microsoft/microsoft-ui-xaml
TreeView Drop Event not triggering for its children TreeView items drag and drop.
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
When using the TreeView object, the Drop event is not triggered correctly. It only works when hovering another TreeView item and putting back the dragged item at its original place.
When dropping an item on another item, or dragging an item and dropping it to a new location, the drop event is never triggered.
### Steps to reproduce the bug
Scenario 1 (triggers the dropped event) :
1. Pick a treeview item to be dragged
2. Drag it slightly over another treeview item
3. Put it back at it's original place.
Scenario 2 (not triggering the drop event) :
1. Pick an item to be dragged
2. drag it directly on another treeview item
3. drop it
Scenario 3 (not triggering the drop event):
1. Pick a treeview item to be dragged
2. drag it to a new location in the treeview (not on another item)
3. drop it
### Expected behavior
All of these scenarios should trigger the Drop event as all the targets location are valid, but they aren't. In my code, only the children treeview item are bound to the TreeViewItem_Drop method. So only those are expected to call the method (those in the FileTemplate datatemplate).
### Screenshots

### NuGet package version
WindowsAppSDK 1.7.250513003
### Windows version
Windows 11
### Additional context
**MainWindow.Xaml*
```XAML
```
**_MainWindow.Xaml.cs_**
```csharp
using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace TreeView_Drop
{
public sealed partial class MainWindow : Window
{
public ObservableCollection DataSource { get; set; }
public MainWindow()
{
this.InitializeComponent();
DataSource = GetData();
MainGrid.DataContext = this;
}
private ObservableCollection GetData()
{
return new ObservableCollection
{
new ExplorerItem
{
Name = "Documents",
Type = ExplorerItem.ExplorerItemType.Folder,
Children =
{
new ExplorerItem
{
Name = "ProjectProposal",
Type = ExplorerItem.ExplorerItemType.File,
},
new ExplorerItem
{
Name = "BudgetReport",
Type = ExplorerItem.ExplorerItemType.File,
},
},
},
new ExplorerItem
{
Name = "Projects",
Type = ExplorerItem.ExplorerItemType.Folder,
Children =
{
new ExplorerItem
{
Name = "Project Plan",
Type = ExplorerItem.ExplorerItemType.File,
},
},
},
};
}
private void TreeViewItem_Drop(object sender, DragEventArgs e)
{
Debug.WriteLine("Dropped");
}
}
public class ExplorerItem
{
public enum ExplorerItemType
{
Folder,
File,
}
public string Name { get; set; }
public ExplorerItemType Type { get; set; }
public ObservableCollection Children { get; set; } = new ObservableCollection();
}
class ExplorerItemTemplateSelector : DataTemplateSelector
{
// Template to use for folder items in the TreeView.
public DataTemplate FolderTemplate { get; set; }
// Template to use for file items in the TreeView.
public DataTemplate FileTemplate { get; set; }
// Determines which template to use for each item in the TreeView based on its type.
protected override DataTemplate SelectTemplateCore(object item)
{
// Cast the item to the ExplorerItem type.
var explorerItem = (ExplorerItem)item;
// Return the appropriate template: FolderTemplate for folders, FileTemplate for files.
return explorerItem.Type == ExplorerItem.ExplorerItemType.Folder
? FolderTemplate
: FileTemplate;
}
}
}
```
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior from MainWindow.Xaml and MainWindow.Xaml.cs using the TreeView, TreeViewItem_Drop handler, and the three drag-and-drop scenarios. Start by checking how TreeView and its child TreeViewItem elements route Drop events. Done means valid drops on another item or a new tree location trigger the Drop event consistently, not only when returning to the original location.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100