This project is read-only.

Using ModernDialog's buttons to trigger validation

Nov 20, 2013 at 1:19 PM
I needed to display a small modal dialog to ask a few data fields. I implemented it by creating a XAML (NewPersonView) class inheriting from ModernDialog.

I'm using Caliburn Micro for MVVM and so I have the NewPersonViewModel where I have properties corresponding to the Textboxes in my view as well as DataAnnotation attributes for the properties validation.

Everything is working fine except for 2 things:
  • The width of the dialog is too small and I can't see the validation text correctly. Is there a way to know the size of those validation, or resize the dialog when they're displayed?
  • If I try to use the "standard" OKButton and CancelButton that are defined in ModernDialog, how can I tell them to trigger the validation? And how can I disable the OKButton until all the validation has been fixed?
Finally, I'm displaying my modal dialog like this:
NewPersonViewModel np = new NewPersonViewModel();
var result = windowManager.ShowDialog(np);
if (result.HasValue && result.Value == true)
   this.people.Add(new Person(np.Age)
      Name = np.Name
But for some reason, result.Value is always false, even if I click "OK" and not "Cancel".
Jan 7, 2014 at 4:40 PM
Edited Jan 7, 2014 at 4:47 PM
1: you have to re-build a custom version of your modernUI library to remove the MaxWidth, MaxHeight, and SizeToContent attributes in ModernDialog.xaml. Not really sure why there were size restrictions enforced.

2: in your subclass of ModernDialog, override the OkButton's command with one of your own. (I'm using a different RelayCommand, but you should get the idea) For example:
    private void OnLoaded(object sender, RoutedEventArgs e)
        this.OkButton.Command = new RelayCommand<object>(o => ExecuteOkBtnClick(o), IsOkEnabled); 
private void ExecuteOkBtnClick(object o)
    if (Validate())
        var result = o as MessageBoxResult?;
        if (result.HasValue)
            Result = result.Value;
private bool IsOkEnabled(object arg)
        return !cFileTxt.GetBindingExpression(TextBox.TextProperty).HasError &&
               !xTxt.GetBindingExpression(TextBox.TextProperty).HasError &&
               !yTxt.GetBindingExpression(TextBox.TextProperty).HasError &&
               !zTxt.GetBindingExpression(TextBox.TextProperty).HasError &&
3: while you're adjusting the ModernDialog's code for item 1, do the change outlined here and then you will get a proper return value for DialogResult. (in fact, you will have to do the change to get the code for item 2 to work)