处理RoutedEvent继续冒泡树 [英] Handled RoutedEvent continues to bubble up tree
问题描述
我正在开发一个 TreeView控件
为基础的控制和我双击事件不断冒泡我的树型视图
节点。
I'm developing a TreeView
based control and my double click event continues to bubble up my TreeViewItem
nodes.
我们的目标是让树型视图
展开或当它被双击崩溃。
The goal is to have the TreeViewItem
expand or collapse when it is double clicked.
我有一个适用的 MouseDoubleClick
事件每个事件处理程序的样式树型视图
。
I have a style that applies an event handler for the MouseDoubleClick
event to each TreeViewItem
.
下面是处理该事件。
private void TreeViewItemDoubleClicked( object sender, RoutedEventArgs e )
{
// Get the specific tree view item that was double clicked
TreeViewItem treeViewItem = sender as TreeViewItem;
// not null?
if( null != treeViewItem )
{
// Switch expanded state
if( true == treeViewItem.IsExpanded )
{
treeViewItem.IsExpanded = false;
}
else
{
treeViewItem.IsExpanded = true;
}
// Set event handled
e.Handled = true; // [1]
}
}
这为顶工作正常水平树型视图
然而,当一个孩子被双击,事件冒泡树造成整支崩溃。为什么事件继续泡沫?由于注意到 [1]
我设置为已处理该事件。
This works fine for the top level TreeViewItem
however when a child is double clicked, the event bubbles up the tree causing the entire branch to collapse. Why is the event continuing to bubble? As noted a [1]
I'm setting the event as handled.
推荐答案
讨厌回答我的问题,但这里是我最终来到了使用该解决方案。
Hate answering my own questions but here is the solution that I ultimately came to use.
跨指定的MouseDoubleClick提高,以适应每个数源的后树型视图的分支(从小孩到根),无论如果事件处理利用我从这个问题的答案:
After coming across a few sources that specified that the MouseDoubleClick is raised for each TreeViewItem in the branch ( from child up to the root ) regardless if the event is handled I utilized the answer from this question:
的http://stackoverflow.com/questions/2941762/wpf-treeview-get-treeviewitem-in-previewmousedown-event一>
获得,这是鼠标事件下的树型视图。如果当前发送者等于鼠标事件的的TreeViewItem根据需要我展开/折叠。否则,我只是忽略事件,什么也不做。
to get the TreeViewItem that was under the mouse event. If the current sender is equal to the TreeViewItem of the mouse event I expand/collapse as required. Otherwise, I just ignore the event and do nothing.
这篇关于处理RoutedEvent继续冒泡树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!