views:

84

answers:

2

If I handle the PreviewMouseDown event on TreeViewItem, I get events for everything I click on that TreeViewItem include the little +/- box to the left (Windows XP). How can I distinguish that?

I tried the following:

// We've had a single click on a tree view item
// Unfortunately this is the WHOLE tree item, including the +/-
// symbol to the left. The tree doesn't do a selection, so we
// have to filter this out...
MouseDevice device = e.Device as MouseDevice;

// This is bad. The whole point of WPF is for the code
// not to know what the UI has - yet here we are testing for
// it as a workaround. Sigh...

// This is broken - if you click on the +/- on a item, it shouldn't
// go on to be processed by OnTreeSingleClick... ho hum!
if (device.DirectlyOver.GetType() != typeof(Path))
{
    OnTreeSingleClick(sender);
}

What I'm after is just single click on the tree view item excluding the extra bits it seems to include.

A: 

If my understanding is correct, e.OriginalSource should contain the control you actually clicked on. Make sure it's a TextBlock (or whatever is actually in your TreeView). You could also use something like this post to determine which TreeViewItem the OriginalSource belongs to, if you're paranoid.

JustABill
What I'm looking for is a means to detect if I've clicked on the +/- button to the left of the TreeViewItem. What I'm seeing is Border or Path. Unfortunately, if you click on a text block just outside of the text block, you get a Border. So I cannot distinguish between clicking on the TreeViewItem or the +/-. The only unique thing I'm seeing is Path - however, if I use a Path item inside a TreeViewItem, how do I tell them apart?
imekon
Perhaps set the Tag property of anything you put as the "content" of the TreeViewItem in order to distinguish it from something else?
JustABill
A: 

My solution is to use the SelectionItemChanged event, seems to avoid issues with the +/- button.

imekon