Modal dialog focus problems on WPF application
I have a problem with my wpf application where a modal dialog will appear behind the main application causing it to hang.
It is very inconsistent, where most of the time the pop-up works correctly and is shown in front but occasionally it will pop-up behind, which stops any interaction with it. I开发者_开发技巧 can still close the dialog using the taskbar if this happens. I have noticed that it generally occurs when lots of other applications are open and the taskbar is full. Also, I am working with two screens and the problem only occurs on the screen with the taskbar - very wierd!
The dialog is a window control, which contains an injected usercontrol as it's data context. To set the owner of the window before calling ShowDialog(), the first active window in the application is used instead of Window.GetWindow(this):
private static Window GetOwner()
{
if (Application.Current != null)
{
var owner = Application.Current.Windows.Cast().FirstOrDefault(w => w.IsActive);
return owner ?? Application.Current.MainWindow;
}
return null;
}
Any ideas of what may be causing this problem? or even how to try and track it so I can gather more information when it happens?
Thanks, Donal
I had out of focus issue on a Windows Form application which sometimes translated to flickering effect. the reason was one of the guys had the code modified for Hiding the modal window until its shown to avoid the ugly dark patch! Since he called .Hide() on the top most modal window the focus went to the other application and when he called the .Show() when the window has fully loaded it came into focus with an outoffocus &or flickering.
it took me a while to nail this issue and change the logic in altering the opacity. i made the window transparent until its fully loaded. Check out if this helps you in any ways!
I just had a similar occurrence with my WPF application because the Owner property of the modal opening Window was not set properly. It still worked kinda, sorta most of the time, but many times the modal window would be stuck behind, just as you described, especially when switching between multiple application windows.
I added my own ShowDialog() method to the modal window, which looked like this:
public bool? ShowDialog(Window owner)
{
Owner = owner;
return ShowDialog();
}
And I explicitly passed in the owning Window when showing it. By setting the Owner property properly, my problems were fixed.
Perhaps you have not set the Owner property properly as well?
精彩评论