简体   繁体   中英

Wpf prevent Popup window from removing the focus of an Textbox

In my WPF-Application I use a small window to display some notifications, but when the notificationwindow shows up, the focus in the mainwindow gets lost. For example when I'm writing in a TextBox and I'm receiving a notification (the notification window shows up), the textbox loses the focus and I have to click in the textbox again to continue my Work.

So how can I keep the focus of the mainwindow, when the notificationwindow shows up? I already tried to set Focusable="false" in the notificationwindow

It's not really a case of how can I stop some control from becoming focused? , rather how can I focus my control after it has lost focus? Now, as @ AdrianFaciu mentioned, it's rather difficult to go into details when you have omitted all relevant information from your question. One option would be to set focus to your control whenever it loses focus:

private void OnControlLostFocus(object sender, RoutedEventArgs e)
{
    YourControlType control = sender as YourControlType;
    if (control != null) control.Focus();
}

There is a ShowActivated property on Window object so you can keep the main window activated after you show a child window.

From MSDN:

When a window with its ShowActivated property set to false is opened, the window is not activated and its Activated event is not raised until a user manually activates the window by selecting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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