至于Validation.Error事件的点火顺序是关心我得到奇怪的行为。 根据该文件在这里 , 数据绑定引擎首先去除可能已被添加到附接结合的元件的特性的任何Validation.Errors ValidationError。 因此, 删除了ValidationErrorEvent应添加之前被解雇,但奇怪的是在我的情况下添加的事件被被去除的事件之前触发。 下面是我使用的代码。
XAML
<TextBox Grid.Row="3" Grid.Column="1" Name="txtGroupsPerRow" >
<TextBox.Text>
<Binding Path="StandardRack.NoOfGroupsPerRow" ValidatesOnDataErrors="True" NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<gs:NumericValidationRule PropertyName="No Of Groups Per Row"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
代码隐藏
private RoutedEventHandler _errorEventRoutedEventHandler;
private void UserControl_Loaded(object sender, RoutedEventArgs e) {
_errorEventRoutedEventHandler = new RoutedEventHandler(ExceptionValidationErrorHandler);
this.AddHandler(System.Windows.Controls.Validation.ErrorEvent, _errorEventRoutedEventHandler, true);
}
private void UserControl_Unloaded(object sender, RoutedEventArgs e) {
this.RemoveHandler(System.Windows.Controls.Validation.ErrorEvent, _errorEventRoutedEventHandler);
_errorEventRoutedEventHandler = null;
}
//Added fired before Removed
private void ExceptionValidationErrorHandler(object sender, RoutedEventArgs e) {
if (validationErrorEvent.Action == ValidationErrorEventAction.Added) {
viewModel.AddValidationError(propertyPath, validationErrorEvent.Error.ErrorContent.ToString());
}
else if (validationErrorEvent.Action == ValidationErrorEventAction.Removed) {
viewModel.RemoveValidationError(propertyPath);
}
}
有没有人碰到这个问题,或者是有什么错我的代码?