gpt4 book ai didi

wpf - Validation.Error 事件的奇怪触发顺序 - 添加在删除之前触发

转载 作者:行者123 更新时间:2023-12-02 05:06:39 26 4
gpt4 key购买 nike

就 Validation.Error 事件的触发顺序而言,我遇到了奇怪的行为。根据文档here , 数据绑定(bind)引擎首先删除可能已添加到绑定(bind)元素的 Validation.Errors 附加属性中的任何 ValidationError。因此,Removed 的 ValidationErrorEvent 应该在 Added 之前触发,但奇怪的是,在我的情况下,Added 事件在 之前触发删除事件。这是我正在使用的代码。

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);
}
}

有没有人遇到过这个问题,或者我的代码有什么问题?

最佳答案

查看 source code , 在删除旧验证错误之前添加新验证错误的步骤

are carefully ordered to avoid going through a "no error" state while replacing one error with another

关于wpf - Validation.Error 事件的奇怪触发顺序 - 添加在删除之前触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10629278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com