I have a treeview and im trying to implement a drag and drop functionallity.
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<EventSetter Event="TreeViewItem.PreviewMouseLeftButtonDown" Handler="PreviewMouseLeftButtonDown" />
</Style>
</TreeView.ItemContainerStyle>
Then i have an event in the code behind to handle the click, and start the drag:
void PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is TreeViewItem)
{
//clone the tree item, create an adorner, apply adorner
//doDragDrop
}
}
My problem is since PreviewMouseLftButtonDown is a routed event using the tunneling strategy, when the mouse is clicked on a treenode item, I get the root node first. I am not interested in the root node, only the leaf nodes. My structure is like this:
HeaderNodeObject
|_LeafnodeObject
|_LeafNodeOject
So in my event I need to basically not do anything and just proceed if the sender treeViewItem is a HeaderNodeObject. But how can i tell what it is? the sender comes in as a treeViewItem, im not sure how to tell what the object it holds is.
I solved this fairly simply by just checking if the itemsource on the sender is null. If its null that means its a leaf node:
if (sender is TreeViewItem)
{
Item = sender as TreeViewItem;
//make sure we have a leaf node, if we dont just move on.
if (Item.ItemsSource == null)
{
//do my stuff
}
}