当选择子节点时,WPF被选中阻止触发

本文关键字:子节点 选择 WPF | 更新日期: 2023-09-27 18:17:16

我遇到了一个问题,我不确定是否有可能防止它。我想它默认是这样设计的。

我已经编码了一个由XML填充的树视图列表,每个节点,当被选中时,正在填充一些文本框。根据它们的类型,它将触发不同的函数。

问题是,当我选择一个孩子,它似乎触发"isselection"为所有的父母treeviewitem一直到顶部,这反过来触发相关的功能,以及,我不想要的。

是否知道如何防止IsSelected的这种"反向继承"?

示例(检查下面的代码):选择"node"将触发"Node_Selected", "Dialog_Selected", "Actor_Selected"。

谢谢你的帮助。

最诚挚的问候,

仅供参考:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        XmlDocument document = new XmlDocument();
        document.Load("XML/ActorsDialogs.xml");
        XmlNodeList actors = document.SelectNodes("/dialogs/actor");
        foreach (XmlNode actor in actors)
        {
            TreeViewItem newActor = new TreeViewItem();
            newActor.Header = actor.SelectSingleNode("actorname").InnerText;
            newActor.Selected += new RoutedEventHandler(Actor_Selected);
            XmlNodeList dialogs = actor.SelectNodes("dialog");
            foreach (XmlNode dialog in dialogs)
            {
                TreeViewItem newdialog = new TreeViewItem();
                newdialog.Header = "Dialog:" + dialog.SelectSingleNode("dialogID").InnerText;
                newdialog.Selected += new RoutedEventHandler(Dialog_Selected);
                BuildNodes(newdialog, dialog);
                newActor.Items.Add(newdialog);
            }
            ActorsList.Items.Add(newActor);
        }
    }
    private void BuildNodes(TreeViewItem treeNode, XmlNode parentElement)
    {
        foreach (XmlNode child in parentElement.ChildNodes)
        {
            if (child.Name == "node" || child.Name == "reply")
            {
                XmlElement childElement = child as XmlElement;
                TreeViewItem childTreeNode = new TreeViewItem();
                string ID = child.SelectSingleNode(child.Name + "ID").InnerText;
                childTreeNode.Header = childElement.Name + ":" + ID;
                switch (child.Name)
                {
                    case "node":
                        childTreeNode.Selected += new RoutedEventHandler(Node_Selected);
                        break;
                    case "reply":
                        childTreeNode.Selected += new RoutedEventHandler(Reply_Selected);
                        break;
                    default:
                        break;
                }

                treeNode.Items.Add(childTreeNode);
                BuildNodes(childTreeNode, childElement);
            }
        }
    }
     private void Actor_Selected(object sender, RoutedEventArgs e){}
     private void Dialog_Selected(object sender, RoutedEventArgs e){}
     private void Node_Selected(object sender, RoutedEventArgs e){}
     private void Reply_Selected(object sender, RoutedEventArgs e){}

当选择子节点时,WPF被选中阻止触发

在事件处理程序中,您可以设置e.h handled = true。这将防止事件冒泡到树。

private void Node_Selected(object sender, RoutedEventArgs e)
{
   e.Handled = true; //this will prevent the event from bubbling up to parents;
   //Do the rest of the code here.
}

参见https://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx了解更多关于RoutedEvents的信息。这些事件包括向上的冒泡事件和向下的隧道事件。