Take the 2-minute tour ×

My UI is simple. In a Silverlight 5.0 application, I'm using MVVM and I let the user adds many textboxes as he wants to add in a ObservableCollection<Model> and a Button.

Model just have one property and its datatype is an integer.

The data template for this model is just a simply textbox.

<TextBox Text="{Binding Number}" />

So the idea is, when all the textboxes does not have any error, the command is enabled, but if any model has an error, the command should be disabled.

How can I implement this validation? Thanks in advance.

share|improve this question
add comment

1 Answer

You can simply throw an exception in appropriate property`s setter:

public int Number
    get {//...}
    set {
           if(value >= 10)
             throw new Exception("Number should be less than 10");
           _number = number;


And your binding should be:

<TextBox Text="{Binding Number, Mode="TwoWay" ValidateOnExceptions="True"}" />

FrameworkElement has BindingValidationErrorEvent, which can be used for implement enable/disable command logic. Remember to set NotifyOnValidationError to True for your binding.

p.s.Also, i suggest you read about INotifyDataErrorInfo

share|improve this answer
Ohh I see, so which the last interface, I can check in my viewModel if my models has an error right? –  Darf Zon Nov 14 '12 at 6:26
if you implement INotifyDataError or setter (as in example below) in your Model class you can validate it. –  Dmitry Martovoi Nov 14 '12 at 7:00
add comment

Your Answer


