简体   繁体   English

C#新窗口焦点

[英]C# New window focus

My program contains a listview with "SelectionChanged" event. 我的程序包含一个带有“SelectionChanged”事件的列表视图。 When I'm changing the selected item in the list view I want to open a new window with specific options related to it. 当我在列表视图中更改所选项目时,我想打开一个新窗口,其中包含与其相关的特定选项。 But the problem is that the new window appears for a moment and it hides behind the main window. 但问题是新窗口出现片刻,它隐藏在主窗口后面。 This is a part of my code: 这是我的代码的一部分:

private void display_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var editwindow = new EditWindow();
        editwindow.Show();
        editwindow.Focus();
    }

I've also include Focus function in second window when it's initiated. 当它启动时,我还在第二个窗口中包含了Focus功能。 How I could solve this problem? 我怎么能解决这个问题?

Trying to change the focus while an event is fired is often troublesome, it is here. 尝试事件发生时更改焦点通常很麻烦,就在这里。 You'd need a PostSelectionChanged event but that's not available. 你需要一个PostSelectionChanged事件但是不可用。 You can create your own though, Dispatcher.BeginInvoke() can run the target after all the event processing is completed. 您可以创建自己的,Dispatcher.BeginInvoke()可以在完成所有事件处理后运行目标。 This solves your problem: 这解决了您的问题:

private void display_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    Dispatcher.BeginInvoke(new Action(() => {
       var editwindow = new EditWindow();
       editwindow.Show();
    }));
}

使用Activate()函数:

editWindow.Activate();

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

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