Apologies in advance if this has already been asked; I have spent a while Googling and searching stack overflow - but can't find a similar question.
I have a WPF window which has many, many data entry controls. All the controls have two way bindings to the view model, and validate using IDataErrorInfo.
An example of one the bindings is given here:
<TextBox >
<TextBox.Text>
<Binding Path="Street" Mode="TwoWay" NotifyOnValidationError="True" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
The only difference between this binding an all the others is the Path.
All my bindings require the validation rule, and the instruction about when to update as there is a lot of cross field validation going on.
My question is - can I apply the same binding to a textbox without all the copy/pasting I am currently having to do for the above example?
I was thinking that maybe I should roll my own subclass of binding to take care of it - but I have no idea if this is good practice or not?
Update: I've just tried a subclass of the binding like so:
public class ExceptionValidationBinding : Binding
{
public ExceptionValidationBinding()
{
Mode = BindingMode.TwoWay;
NotifyOnValidationError = true;
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
ValidatesOnDataErrors = true;
ValidationRules.Add(new ExceptionValidationRule());
}
}
which makes my xaml look like this:
<TextBox Text="{bindings:ExceptionValidationBinding Path=PostCode}" />
And it seems to work... like I said - no idea if there are any problems with this approach though.