父树视图项幽灵选择事件! [英] Parent TreeView Item ghost selected event!

查看:25
本文介绍了父树视图项幽灵选择事件!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TreeView,它会在引发每个 TreeViewItems Selected 事件时启动一个新窗口.

I have a TreeView that launches a new window when each of its TreeViewItems Selected event is raised.

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"
    Height="300"
    Width="300">
<Grid>
    <TreeView  Name="treeView1">
        <TreeViewItem Header="Root">
            <TreeViewItem Header="Parent" Selected="ParentTreeViewItem_Selected">
                <TreeViewItem Header="Child" Selected="TreeViewItem_Selected" ></TreeViewItem>
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>
</Grid>
</Window>

背后的代码

namespace WpfApplication1

{公共部分类 Window1 : 窗口{公共窗口 1(){初始化组件();}

{ public partial class Window1 : Window { public Window1() { InitializeComponent(); }

    private void TreeViewItem_Selected(object sender, RoutedEventArgs e)
    {
        Window w = new Window();
        w.Show();
        e.Handled = true;
    }

    private void ParentTreeViewItem_Selected(object sender, RoutedEventArgs e)
    {

    }
}

}

当我点击子节点时,新窗口会按预期启动.然而,紧接着它的父节点 Selected 事件被触发,从新窗口窃取焦点,并将父节点标记为当前选择!

When I click on the child node the new window is launched as expected. However imediatly afterwords its parents Selected eventis fired stealing focus from the new window, and marking the parent node as the current selection!

我的期望是新启动的窗口会有焦点,被点击的节点会变成灰色,向用户表明他/她的选择.有没有人知道为什么会发生这种情况以及我该如何预防?

My expectation was that the newly launched window would have focus, and the node that was clicked would turn gray indicating to the users his/hers selection. Does anyone have any idea of why this happens and how I can prevent it?

谢谢,布雷特

推荐答案

我想我会发布答案.我终于找到了解决这个问题的方法.设置 w.Owner = this;没有效果.原来在 TreeViewItem 的 Selected 事件上启动一个新窗口会导致一些焦点问题.我还没有通过在 Dispatcher 上执行这个来找出根本原因,这似乎纠正了它.见下文

Thought I would post the answer. I finally found a way around this. Setting w.Owner = this; has no effect. Turns out launching a new window on the Selected event of a TreeViewItem causes some focus issues. I have not found out what the root cause is by executing this on the Dispatcher seems to correct it. See Below

    private void ChildTreeViewItem_Selected(object sender, RoutedEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => new Window().Show()));
    }

希望这可以为其他人节省一些时间.

Hope this saves someone else some time.

布雷特

这篇关于父树视图项幽灵选择事件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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