1

I need to add ValidationRule to the TextBox programmatically in WPF. I'm trying something like:

Binding binding = BindingOperations.GetBinding(myTextBox, TextBox.TextProperty);
binding.ValidationRules.Clear();
binding.ValidationRules.Add(myValidationRule);

But BindingOperations.GetBinding return null. Why? Many thanks.

mxpv
  • 946
  • 2
  • 10
  • 31

1 Answers1

0

From the API:

Return Value

Type: System.Windows.Data.Binding The Binding object set on the given property or Nothing if no Binding object has been set.

So apparantly there is no Bindig object set.

Source

Onots
  • 2,118
  • 21
  • 28
  • Can I somehow add validation rule without binding? – mxpv Dec 30 '11 at 13:26
  • I would advise you to read the article at this link: http://blogs.msdn.com/b/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx – Onots Jan 02 '12 at 09:21