How to prevent a new WPF form from stealing focus?

旧城冷巷雨未停 提交于 2019-12-17 11:29:42

问题


I have written a simple MSN-style program that will send and retrieve messages using WCF. The main form contains a Textbox to type in the message to be sent. In the background the application polls the server every few seconds for new messages. When a new message is received a new window is opened to display it. This has to be done on the UI thread using the Dispatcher class.

The problem is that when the new window is shown, the focus shifts away from the TextBox, so that typing is interrupted. This is very annoying! In MSN Messenger it is possible to continue typing your own message while receiving one. How is it done?

As a workaround I postpone the popup with the new message while the TextBox has focus, but there should be a better way!


回答1:


The answer is simple: Since .NET 3.5 SP1 WPF forms have a ShowActivated property. Set this to false and any form thus marked won't steal no focus no more!




回答2:


You could set the Focusable property of the window to false.




回答3:


In my application I need to show a notification Window on top of all other windows while my MainWindow is minimized, but without stealing the focus.

So I just do this:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    this.Topmost = true;
    this.Topmost = false;
}


来源:https://stackoverflow.com/questions/1456529/how-to-prevent-a-new-wpf-form-from-stealing-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!