0

My program has got tens of TextBoxes, which all could use one common Validation Rule. The rule itself is not very complex: it would only prevent the use of a semicolon (;).

Does there exist any way to implement it (more easily than applying the same rule manually to all TextBoxes)?

Edit: I cannot see how it could be related to How to use IDataErrorInfo.Error in a WPF program? - I have no problems applying the validation to a TextBox (or displaying the error in a ToolTip).

Alex
  • 33
  • 7

1 Answers1

0

You could write a custom markup extension that extends Binding and adds the ValidationRule:

public class CustomBinding : Binding
{
    public CustomBinding(string path)
        : base(path)
    {
        this.ValidationRules.Add(new YourValidationRule())
    }
}

You would then use this one instead of the usual {Binding} when you set up your bindings in XAML, e.g.:

<TextBox Text="{local:CustomBinding SourceProperty}" />
mm8
  • 163,881
  • 10
  • 57
  • 88
  • @Alex: Did you try this? – mm8 Mar 15 '19 at 15:26
  • It seems that it is impossible to create what I am asking without some additions to the XAML of the TextBoxes. This looks like to be the most simple solution, I'll give it a go. Thanks! – Alex Mar 18 '19 at 08:08