In my app, I'm loading a DocumentViewer for previewing and printing. Because loading can take up to a few seconds, I would like to display a ProgressDialog. The logic is this: display ProgressDialog, create and display DocumentViewer, close ProgressDialog.
The problem is that when closing the dialog, the focus returns to the MainWindow, instead of remaining to the DocumentViewer window. I think it's because the dialog CloseAsync(). I've tried calling Focus(), Activate(), Show(), setting TopMost = true, setting the owner of the child window, but, although the window is initially displayed in front of the parent, the focus still returns to the parent. The only way to I restored the focus to the child was by doing a programmatic click inside the child window, in ProgressDialogController.Closed event, but the disadvantage is that there is an ugly transition (child initially appears in front, then parent, then child again).
Can CloseAsync() be avoided? Or how to close MahApps ProgressDialog without losing focus on child control?
The ProgressDialog should be closed without losing focus from the child window.
I don't think it's possible currently:
When the dialog is closed, DialogManager.RemoveDialog is called, and when there's no other dialog open, it calls MetroWindow.RestoreFocus.
RestoreFocus now sets the focus back to the element, which was focused when the dialog opened.
@punker76 Correct me if I'm wrong, but I guess a method like following could help. It's just an idea...
public void ResetStoredFocus()
{
restoreFocus = null;
}
@thoemmi This is maybe a possible solution. Or an additional (public) version of the SetFocus method with a parameter which overrides the stored control...
@thoemmi Yes, it works! I tested it and focus is given to the child control.
@dxdx347 2 new methods now available on MetroWindow 12557bd6f5856831df409cc02cc59c5c61d5b506
/// <summary>
/// Stores the given element, or the last focused element via FocusManager, for restoring the focus after closing a dialog.
/// </summary>
/// <param name="thisElement">The element which will be focused again.</param>
public void StoreFocus([CanBeNull] IInputElement thisElement = null)
/// <summary>
/// Clears the stored element which would get the focus after closing a dialog.
/// </summary>
public void ResetStoredFocus()
^ @thoemmi
Looks good to me 馃憤