C # – WPF DataBinding verification is ignored

I (believe) I am connecting data binding validation in a textbook way, but it doesn’t work at all.

In the debugger, forever The Validate(object value, CultureInfo cultureInfo) method will not be called.

What is assigned? Also, for bonus points, any pointers to debugging WPF would be great.

I posted my XAML and the course in question

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/ winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/ expression/blend/2008"
xmlns:local="clr-namespace:Foo.Controls"
mc:Ignorable="d"
d:DesignWidth="300">














< /Binding>








Validity rules

< p>

public class ScalarValidationRule: ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string number = value as string;
double d;
return new ValidationResult(Double.TryParse(numb er, NumberStyles.Any, cultureInfo, out d),
String.Format("\"{0}\" is not a number.", number));
}
}< /pre>
You are adding the ValidationRule to the one-way binding. One-way binding never sets the value Push to the source, so there is no need to validate anything, and ValidationRule will never be called. If you want to validate the data entered by the user, set Mode=“TwoWay” on the binding. You can also omit the Mode property completely, because TextBox.Text defaults The binding is two-way.

If you really need one-way binding and want to validate when pushing the value from the source (Scalar) to the target (TextBox.Text), then on the ValidationRule Set ValidatesOnTargetUpdated to True. You can do this in XAML or in the constructor of the ValidationRule class.

I (believe) I am connecting data in a textbook way Binding validation, but it doesn’t work at all.

In the debugger, the Validate(object value, CultureInfo cultureInfo) method is never called.

What gives NS? Also, for bonus points, any pointers to debugging WPF would be great.

I posted my XAML and the course in question

 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/ winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/ expression/blend/2008"
xmlns:local="clr-namespace:Foo.Controls"
mc:Ignorable="d"
d:DesignWidth="300">














< /Binding>








Validity rules

< p>

public class ScalarValidationRule: ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string number = value as string;
double d;
return new ValidationResult(Double.TryParse(number, Nu mberStyles.Any, cultureInfo, out d),
String.Format("\"{0}\" is not a number.", number));
}
}

You are adding a ValidationRule to a one-way binding. One-way binding never pushes the value to the source, so there is no need to validate anything, and never Call ValidationRule. If you want to validate the data entered by the user, please set Mode = "TwoWay" on the binding. You can also omit the Mode attribute completely, because TextBox.Text is bound to two-way by default.

If you really need one-way binding and want to validate when the value is pushed from the source (Scalar) to the target (TextBox.Text), then set ValidatesOnTargetUpdated to True on the ValidationRule. You can either in XAML or in ValidationRule Do this in the constructor of the class.

Leave a Comment

Your email address will not be published.