【问题标题】:Bring parent window to front when child window selected from taskbar从任务栏选择子窗口时将父窗口置于前面
【发布时间】:2012-06-19 14:29:28
【问题描述】:

标题几乎说明了一切,但基本上我有一个主父窗口,它偶尔会打开子窗口。现在可以从 Windows 7 任务栏中选择一个子窗口,并且只有那个窗口会被带到前面。我希望它找到一种将父窗口链接到此命令的方法,以便在任何时候选择子窗口时,父窗口也会自动被带到前面。

我尝试在子窗口“GotFocusevent handler, but neither seemed to make a difference. I also tried theBringIntoView()”方法中同时使用Focus()Topmost = true 命令,但同样没有乐趣。有没有人见过这个或者知道实现这个的方法?

这是我迄今为止尝试过的。首先设置mainWindow 然后立即设置子窗口的逻辑是我仍然希望子窗口具有焦点,但我希望mainWindow 高于任何其他正在运行的程序(即Excel、VS 等) .

    private void Window_GotFocus(object sender, RoutedEventArgs e)
    {
        var mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;

        mainWindow.Topmost = true;
        this.Topmost = true;

    }

【问题讨论】:

  • 刚试了一下,没有任何变化。

标签: c# wpf window focus parent-child


【解决方案1】:

我认为当您的窗口被选中时,您正在收听错误的事件以得到通知。在您的子窗口上订阅Activated 事件,该事件应该让您知道何时在任务栏中选择了您的窗口。从那里您可以激活()您的 MainWindow。

此外,我认为如果您将父窗口设置为子窗口的所有者,您将自动获得此行为。

【讨论】:

  • 我什至不知道Activated,但是将MainWindow 设置为所有者是完全正确的。我知道这是可能的,只是无法放置术语。感谢您让我的大脑快速启动。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-26
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 2012-10-08
  • 2011-10-07
  • 1970-01-01
相关资源
最近更新 更多