I am trying to implement drag and drop for a treeview control that binds to custom elements:
<TreeView ItemsSource="{Binding FirstGeneration}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type NavigationTreeElement}"
ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Margin="1">
<Image Source="open-16x16.png" />
<TextBlock Text="{Binding Path=DisplayName}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
With the underlying class:
public class NavigationElement
{
public string DisplayName { get; set; }
public ObservableCollection<NavigationElement> Children { get; set; }
}
I’m having a problem with associating the rendered TreeViewItem with the underlying NavigationElement. If I find a TreeViewItem that the mouse is over, as such:
private static TreeViewItem GetNearestContainer(UIElement element)
{
// Walk up the element tree to the nearest tree view item.
var container = element as TreeViewItem;
while ((container == null) && (element != null))
{
element = VisualTreeHelper.GetParent(element) as UIElement;
container = element as TreeViewItem;
}
return container;
}
How do I associate that TreeViewItem with an underlying NavigationElement?
I think it’s in
container.Header, odd as that might seem.