Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProgressDialog close causes loss of focus on child control #2532

Closed
dxdx347 opened this issue Jun 2, 2016 · 5 comments
Closed

ProgressDialog close causes loss of focus on child control #2532

dxdx347 opened this issue Jun 2, 2016 · 5 comments
Assignees
Milestone

Comments

@dxdx347
Copy link

dxdx347 commented Jun 2, 2016

What steps will reproduce this issue?

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?

Expected outcome

The ProgressDialog should be closed without losing focus from the child window.

Environment

  • MahApps.Metro v1.3.0.157
  • Windows 10
  • Visual Studio 2015
  • .NET Framework 4.6.1
@thoemmi
Copy link
Collaborator

thoemmi commented Jun 2, 2016

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;
}

@punker76
Copy link
Member

punker76 commented Jun 2, 2016

@thoemmi This is maybe a possible solution. Or an additional (public) version of the SetFocus method with a parameter which overrides the stored control...

@dxdx347
Copy link
Author

dxdx347 commented Jun 2, 2016

@thoemmi Yes, it works! I tested it and focus is given to the child control.

@punker76 punker76 added this to the 1.3.0 milestone Jun 7, 2016
@punker76 punker76 self-assigned this Jun 7, 2016
punker76 added a commit that referenced this issue Jun 7, 2016
@punker76
Copy link
Member

punker76 commented Jun 7, 2016

@dxdx347 2 new methods now available on MetroWindow 12557bd

/// <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

@punker76 punker76 closed this as completed Jun 7, 2016
@thoemmi
Copy link
Collaborator

thoemmi commented Jun 7, 2016

Looks good to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants