就越多的Validation.Error事件触发顺序而言,我越来越奇怪。根据文档here,,数据绑定引擎首先删除可能已添加到绑定元素的Validation.Errors附加属性的任何ValidationError。因此,对于删除的ValidationErrorEvent应先于新增解雇,但奇怪的是在我的情况下删除事件之前的新增事件被炒鱿鱼。这是我正在使用的代码。奇怪的Validation.Error事件触发顺序 - 在Removed之前添加了触发器
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);
}
}
有没有人碰到这个问题,或者是有什么错我的代码?
我刚碰到同样的问题! –