Say I am trying to implement a piece of functionality in which a text box only allows integers to be inputted in by the user. I can implement this two ways, using a ValidationRule that checks whatever the user inputs and binding it to the text property through the XAML or I can create a new behavior and attach it to the control (not through binding).
Examples of the XAML on both:
Behavior:
<TextBox behaviors:DigitsOnlyBehavior.IsDigitOnly="True"/>
ValidationRule which binds to the Window's Text property
<TextBox>
<TextBox.Text>
<Binding RelativeSource = "{RelativeSource Mode=FindAncestor, AncestorType=Window}" Path="Text" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
<Binding.ValidationRules>
<utils:RestrictInputTypeValidator Restriction="IntegersOnly" ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
What are the advantages and disadvantages of these approaches? When should I use them? Or is it a matter of preference?