简体   繁体   English

WPF IsSelected防止选择子项时触发

[英]WPF IsSelected prevent trigger when selecting child

I have a problem I've ran into and I'm not sure if it is possible to prevent it. 我遇到了一个问题,不确定是否可以阻止它。 I supose it is designed like this by default. 我认为默认情况下是这样设计的。

I have coded a treeview list to be filled by a XML and each of these node, when selected, are filling some textbox. 我已经编码了一个由XML填充的树视图列表,并且这些节点中的每一个在被选中时都填充了一些文本框。 Depending on their type, it will trigger a different function. 根据它们的类型,它将触发不同的功能。

The problem is that when I select a child, it seems to trigger "IsSelecting" for all parents treeviewitem all the way to the top which in return trigger the associated function as well and I don't want that. 问题是,当我选择一个孩子时,似乎所有父树的viewviewitem一直触发“ IsSelecting”,这反过来也触发了关联的函数,而我不希望那样。

Any idea how to prevent this "Sort of reverse inheritance" for IsSelected? 任何想法如何防止IsSelected的“反向继承排序”?

Example (check with code below): selecting a "node" will trigger "Node_Selected", "Dialog_Selected", "Actor_Selected". 示例(使用下面的代码检查):选择一个“节点”将触发“ Node_Selected”,“ Dialog_Selected”,“ Actor_Selected”。

Thanks for your help. 谢谢你的帮助。

Best regards, 最好的祝福,

Just for context: 仅出于上下文:

    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){}

In the event handler you can set e.Handled = true. 在事件处理程序中,您可以设置e.Handled = true。 That will prevent the event from bubbling up the tree. 这样可以防止事件冒泡。

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.
}

See https://msdn.microsoft.com/en-us/library/ms742806%28v=vs.110%29.aspx for more information on RoutedEvents. 有关RoutedEvent的更多信息,请参见https://msdn.microsoft.com/zh-cn/library/ms742806%28v=vs.110%29.aspx These include bubbling events which go up the tree and tunneling events which go down the tree. 这些事件包括在树上冒出的冒泡事件和在树上掉下的隧穿事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM