TreeViewItem上的WPF TreeView PreviewMouseDown

时间:2010-06-01 12:57:35

标签: wpf treeview click

如果我在TreeViewItem上处理PreviewMouseDown事件,我会在TreeViewItem上点击所有内容,包括左边的小+/-框(Windows XP)。我怎么能区分呢?

我尝试了以下内容:

// 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);
}

我所追求的只是单击树视图项,不包括它似乎包含的额外位。

2 个答案:

答案 0 :(得分:0)

如果我的理解是正确的,e.OriginalSource应包含您实际点击的控件。确保它是一个TextBlock(或TreeView中的实际内容)。你也可以使用类似this post的东西来确定OriginalSource属于哪个TreeViewItem,如果你是偏执狂。

答案 1 :(得分:0)

我的解决方案是使用SelectionItemChanged事件,似乎避免了+/-按钮的问题。