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

find out if any dialog is open to close it ? #4525

Open
TheCamel opened this issue Oct 30, 2024 Discussed in #4491 · 1 comment
Open

find out if any dialog is open to close it ? #4525

TheCamel opened this issue Oct 30, 2024 Discussed in #4491 · 1 comment

Comments

@TheCamel
Copy link

Discussed in #4491

Originally posted by TheCamel May 29, 2024
Hello,
I have a timer session and would like to close any opened dialog if the session close...
i found the IsAnyDialogOpen but how to get the TDialog and ask for closing ?
Thanks for your help

@IvanGit
Copy link

IvanGit commented Dec 4, 2024

How to get and close a dialog in MahApps.Metro

Getting the current dialog

For IDialogCoordinator:

Task<TDialog?> GetCurrentDialogAsync<TDialog>(object context) where TDialog : BaseMetroDialog;

For MetroWindow:

Task<TDialog?> GetCurrentDialogAsync<TDialog>(this MetroWindow window) where TDialog : BaseMetroDialog;

Closing the dialog

To hide the dialog, use HideMetroDialogAsync with either IDialogCoordinator or MetroWindow.

For IDialogCoordinator:

Task HideMetroDialogAsync(object context, BaseMetroDialog dialog, MetroDialogSettings? settings = null);

For MetroWindow:

Task HideMetroDialogAsync(this MetroWindow window, BaseMetroDialog dialog, MetroDialogSettings? settings = null);

Example Usage

Here's an example of how to get and close a dialog using both IDialogCoordinator and MetroWindow.

Using IDialogCoordinator

var dialogCoordinator = DialogCoordinator.Instance;
var dialog = await dialogCoordinator.GetCurrentDialogAsync<MyCustomDialog>(context);
if (dialog != null)
{
    await dialogCoordinator.HideMetroDialogAsync(context, dialog);
}

Using MetroWindow

var myMetroWindow = Application.Current.MainWindow as MetroWindow;
var dialog = await myMetroWindow.GetCurrentDialogAsync<MyCustomDialog>();
if (dialog != null)
{
    await myMetroWindow.HideMetroDialogAsync(dialog);
}

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

2 participants