为什么TreeViewItem中的项目继承父级的事件处理程序? [英] Why do items in TreeViewItem inherit the event handlers of parent?

查看:141
本文介绍了为什么TreeViewItem中的项目继承父级的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我创建一个TreeViewItem(parentNode),并将TreeViewItem添加到parentNode TreeViewItem中。然后我将一个MouseButtonEventHandler添加到parentNode,现在parentNode中的所有TreeViewItem都有MouseButtonEventHandler。我已经运行调试器来查看我是否有意外写入添加MouseButtonHandler的代码,但是没有...

So, I create a TreeViewItem (parentNode), and I add TreeViewItems into the parentNode TreeViewItem. I then add a MouseButtonEventHandler to the parentNode, and now all the TreeViewItems inside parentNode have the MouseButtonEventHandler. I've run the debugger to see if I have code that was accidentally written to add the MouseButtonHandler, but there isn't...

编辑:我做了其他测试,甚至下降两个级别。有没有办法将事件处理程序隔离到特定节点而不是其子节点和/或父节点?

I did additional tests, and it even goes two levels down. Is there a way to isolate eventhandlers to only the specific node and not its children and/or parents?

public newClass() {
    TreeViewItem parent = new TreeViewItem();

    TreeViewItem childOne = new TreeviewItem();
    addExpandClickListener(childOne);

    TreeViewItem childTwo = new TreeviewItem();
    TreeViewItem childThree = new TreeViewItem();


    childTwo.Items.Add(childThree);
    childOne.Items.Add(childTwo);
    parent.Items.Add(childOne);
    TreeViewObject.Items.Add(parent);
}

private void addExpandClickListener(TreeViewItem item) { item.MouseLeftButtonUp += new MouseButtonEventHandler(item_MouseClick); }


private void item_MouseClick(object sender, MouseButtonEventArgs e) {
    // Define click event as handled
    e.Handled = true;
    if(sender != e.Source) return;
    // Handle click event
    TreeViewItem root = (TreeViewItem)sender;
    if(root.IsExpanded == true) CollapseRecursive(root);
    else root.IsExpanded = true;
    //else root.ExpandSubtree();
    ViewTree.Items.Refresh();
}


推荐答案

parentNode的区域包括儿童的地区。想想重叠的矩形。如果您只想点击工作的文本部分,您将需要提供自己的DataTemplate或使用可视化树来查找标题并应用点击。

The area of the parentNode includes the areas of the children. Think of overlapping rectangles. If you only want to have a click to work for the text part, you'll either have to supply your own DataTemplate or use the visual tree to find the header and apply the click to that.

这篇关于为什么TreeViewItem中的项目继承父级的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆