I would like to change the base template of the MahApps.Metro dialogs (or create a new dialog type), because I would like to show them in a narrow login window. Right now almost all the second words in the message are in a new row, but there are nice big spaces on the right and the left side, which I would like to reduce.
I've found in BaseMetroDialog.xaml
that the message dialog is divided into three parts vertically: 25% space on left side, 50% for the content and 25% space on the right side. I would like to change these numbers.
But how could I change the control template of BaseMetroWindow
with my new one?
It took me a little while to work this out but for fellow newbies like me, here is my fully documented solution to creating customised dialog boxes using mahapps and MVVM. There are probably aspects which could be improved but this is what worked for me.
Declare your dialog resource dictionary in the App.xaml so it is available globally
App.xaml
The resource dictionary contains the template replacement code for the custom dialog
DialogResource.xaml
Create a WPF window called UserInputDialog then replace all xaml code with customdialog xaml. Im using Caliburn Micro syntax to bind the buttons to the underlay dialog viewmodel (cal:Message.Attach=). In the case of dialog xaml code I need to manually specify the button bindings as for some reason with Caliburn Micro it is not automatic like in the main view model.
UserInputDialog.xaml
And the code-behind for UserInputDialog:
UserInputDialog.xaml.cs
Create viewmodel class specifically for the user input dialog
UserInputViewModel.cs
Create a separate ICommand class to pass in the external dialog close function via the dialog viewmodel constructor
SimpleCommand.cs
And finally here is the main view model code to display the customized dialog box and process the returned user input:-
MainViewModel.cs
Just create your own style that overrides the dialog
Template
(and add theDialogShownStoryboard
too).The namespace here is
Now use this custom style e.g. for a custom dialog
Screenshot from main demo
Update
With the latest version of MahApps.Metro it's now possible to change e.g. the
MessageDialog
style globally.Hope that helps!
Override metrodialog style and merge the resource to the Metro Window
Another solution is provided on bug tracker: don't use the Content property, use the DialogTop instead. For example:
Put your custom content (e.g. StackPanel) inside DialogTop and you're done.