Let's say I have a ViewModel
class MyViewModel { public DateTime? InvoiceDate { get; set; } }
and this ViewModel is bound to a text box:
<TextBox Text="{Binding InvoiceDate}" />
Now when the user enters 2015/01/01
, InvoiceDate is 2015/01/01. When the user then changes his input to something invalid, e.g. 2015/1234
, InvoiceDate is still 2015/01/01. This makes sense, since 2015/1234
cannot be converted to a DateTime?
.
However, I want to detect this case and prevent the user from executing a command while invalid data (which cannot be converted to the ViewModel type) is entered. How do I detect this case? I'm sure there's a simple one-liner (like this.AllDataBindingsAreValid()
), but I cannot find it.
void MyCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.AllDataBindingsAreValid(); // What's it really called?
}