custom dialog Mahapps ShowMetroDialogAsync with close button

10,942

Solution 1

I needed a custom Input Dialog. So I created a CustomInputDialog class inherited from BaseMetroDialog.

I used this code to call the method:

public async Task<string> ShowCustomDialog(string message, string title)
{
    var metroDialogSettings = new MetroDialogSettings()
    {
        AffirmativeButtonText = "OK",
        NegativeButtonText = "CANCEL",
        AnimateHide = true,
        AnimateShow = true,
        ColorScheme = MetroDialogColorScheme.Accented,
    };

    var dialog = new CustomInputDialog(View, metroDialogSettings)
    {
        Message = message,
        Title = title,
        Input = metroDialogSettings.DefaultText
    };

    return await InvokeOnCurrentDispatcher(async () =>
    {
        await View.ShowMetroDialogAsync(dialog, metroDialogSettings);

        await dialog.WaitForButtonPressAsync().ContinueWith((m) =>
        {
            InvokeOnCurrentDispatcher(() => View.HideMetroDialogAsync(dialog));
        });

        return dialog.Input;
    });
}

Message, Title and Input are dependency properties of CustomInputDialog. This is working at my end.

Solution 2

Add close button to your dialog (which is in Resources) & hook up its Click event to close your dialog. For closing, use this.HideMetroDialogAsync(dialog); where this is MetroWindow instance.

Share:
10,942
Prasetyo Jean
Author by

Prasetyo Jean

Updated on June 14, 2022

Comments

  • Prasetyo Jean
    Prasetyo Jean almost 2 years

    I create Mahapps custom dialog message with this code. the problem is the dialog form not show close button so cant close it. how to show close button ?

            public async void button_Click(object sender, RoutedEventArgs e)    
    
      {      Button btn = (Button) sender;
            //dialog.Resources["CustomDialogTest"];
            string[] id =  btn.Name.ToString().Split('_');
    
            this.MetroDialogOptions.ColorScheme = MetroDialogColorScheme.Accented;
            var dialog = (BaseMetroDialog)this.Resources["CustomDialogTest"];
    
            var mySettings = new MetroDialogSettings()
            {
                AffirmativeButtonText = "OK",
                AnimateShow = true,
                NegativeButtonText = "Go away!",
                FirstAuxiliaryButtonText = "Cancel",               
            };
    
    
             await this.ShowMetroDialogAsync(dialog);
             // this for close the dialog -> await this.HideMetroDialogAsync(dialog);           
        }
    
  • nam
    nam almost 3 years
    How you are closing the dialog?