ich habe das Problem, dass die Methode für die Validierung einer Eingabe nicht getriggert wird. Hierzu habe ich mal eine Testimplementierung gemacht, um das etwas genauer zu verdeutlichen. Das Interface IDataErrorInfo ist im ViewModel korrekt implementiert und wird hier nicht exemplarisch dargestellt.
public class TestTextBox : TextBox
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int?), typeof(TestTextBox),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ValueChangedCallback));
public TestTextBox()
{
this.LostFocus += (sender, args) =>
{
int value;
if (string.IsNullOrWhiteSpace(this.Text)) this.Value = null;
else if (int.TryParse(this.Text, out value)) this.Value = value;
else this.Value = null;
};
}
private static void ValueChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as TestTextBox;
if (control == null) return;
control.Text = string.Format("{0:F0}", control.Value);
}
public int? Value
{
get { return (int)this.GetValue(ValueProperty); }
set { this.SetValue(ValueProperty, value); }
}
}
Im Xaml funktioniert die Bindung gegen Text, gegen Value aber nicht.
<local:TestTextBox Text="{Binding ElementName=Me, Path=TestValue, NotifyOnValidationError=True, Mode=TwoWay}" />
<local:TestTextBox Value="{Binding ElementName=Me, Path=TestValue, NotifyOnValidationError=True, Mode=TwoWay}" />
Hat jemand eine Idee, was in der Implementierung fehlt, sodass es auch mit dem Property Value funktioniert?
Ronny