In my WPF application, I have a treeview. This treeview is bound to a custom class (i.e. not TreeviewItems). So I use a hierarchicalDataTemplate to control how the tree renders.
When my mouse is over a tree view item, I would like to get the Data Object (i.e. my custom class instance) associated with the tree view item. How do I do this?
To clarify - I need the data object (not the UIElement) under the mouse cursor.
Assume my method to retrieve the data object has the following signature:
private object GetObjectDataFromPoint(ItemsControl source, Point point)
{
...
}
Something like this (untested):
private object GetObjectDataFromPoint(ItemsControl source, Point point)
{
//translate screen point to be relative to ItemsControl
point = _itemsControl.TranslatePoint(point);
//find the item at that point
var item = _itemsControl.InputHitTest(point) as FrameworkElement;
return item.DataContext;
}
private object GetObjectDataFromPoint(ItemsControl source, Point point)
{
//translate screen point to be relative to ItemsControl
point = source.TranslatePoint(point, source);
//find the item at that point
var item = source.InputHitTest(point) as FrameworkElement;
return item.DataContext;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With