I tried following code but it gave null reference error for second line. I deleted second line but now it gave same error for third line. Any idea?
Binding binding = BindingOperations.GetBinding(txt_density, TextBox.TextProperty);
binding.ValidationRules.Clear();
binding.ValidationRules.Add(new MainWindow.Float_Positive_ValidationRule());
You haven’t included the relevant XAML so it is hard to say definitively what the issue is.
If you set a break point on the line
binding.ValidationRules.Clear();you will most likely find your binding is null, probably because there has been no binding set on theTextproperty.If there is a valid binding set on the property then the
ValidationRulescollection should be initialised but empty (i.e. not null), so this shouldn’t be your problem, which leaves the binding itself as the problem.If you use this simple example you will see what I mean, just remove the
Textbinding from the XAML to cause the error.