wpf 从 3.5 开始定位到 4.0 版本会影响 IDataErrorInfo 实现
我有一个使用版本 3.5 实现 IDataErrorInfo 的 MVVM 应用程序。我想尝试以 4.0 为目标,因此修改了目标框架设置并更改了一些位(BitmapFrames 等)。大多数事情看起来都很好,并且过程相对轻松,直到我注意到 IDataErrorInfo 的实现受到了影响。
我的验证控件模板如下所示:
<ControlTemplate x:Key="temp__">
<Border BorderBrush="Orange" BorderThickness="2" CornerRadius="4" SnapsToDevicePixels="True">
<DockPanel>
<Image HorizontalAlignment="Left" VerticalAlignment="Center"
Width="16" Height="16" Margin="-20,0,0,0"
Source="{StaticResource ErrorIcon}"
ToolTip="{Binding ElementName=adornedElement,
Path=AdornedElement.(Validation.Errors),
Converter={helper:ValidationErrorsToStringConverter}}"/>
<AdornedElementPlaceholder Name="adornedElement"/>
</DockPanel>
</Border>
</ControlTemplate>
并在如下文本框样式中使用:
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource error_holder}">
在我的 ViewModel 中,GetValidationError(string propertyName) 使用开关根据我的规则验证适当的属性。
问题是一旦验证被触发,它就不会更新。例如,可以根据需要设置某个字段,也可以不设置。加载后,必填字段将被标记为无效,并带有相应的消息。以前,当输入的值仍然无效时,工具提示中的错误消息将会更新。但是,这不再有效,并且错误消息仍然保留为空值消息。
有谁知道 4.0 中 IDataErrorInfo 的实现有什么变化可以解释这个问题吗?知道如何修复它吗?
I've an MVVM application implementing IDataErrorInfo using version 3.5. I want to try and target 4.0 so have amended the Target Framework setting and changed a few bits around (BitmapFrames and the like). Most things seem just fine and the process was relatively painless, until I noticed that the implentation of IDataErrorInfo has been affected.
My control template for validation looks like this:
<ControlTemplate x:Key="temp__">
<Border BorderBrush="Orange" BorderThickness="2" CornerRadius="4" SnapsToDevicePixels="True">
<DockPanel>
<Image HorizontalAlignment="Left" VerticalAlignment="Center"
Width="16" Height="16" Margin="-20,0,0,0"
Source="{StaticResource ErrorIcon}"
ToolTip="{Binding ElementName=adornedElement,
Path=AdornedElement.(Validation.Errors),
Converter={helper:ValidationErrorsToStringConverter}}"/>
<AdornedElementPlaceholder Name="adornedElement"/>
</DockPanel>
</Border>
</ControlTemplate>
and is used in a textbox style like this:
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource error_holder}">
In my ViewModel, GetValidationError(string propertyName) uses a switch to validate the appropriate property based on my rules.
The issue is that once a validation has been fired it does not update. For instance, a field can be set as required or not. When loaded, the fields that are required are marked as invalid with an appropriate message. Previously, when a value was entered that was still invalid the error message in the tooltip would update. However this no longer works and the error message still remains as a null value message.
Does anyone know of any changes in the implementation of IDataErrorInfo in 4.0 that may account for this? Any idea how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看不到你的绑定,但你应该添加 ValidatesOnDataErrors。
i cant see your binding but you should add ValidatesOnDataErrors.
好吧,我已经解决了这个问题。我没有使用使用 Validation.Errors[0] 的默认实现,该实现在错误修复时会给出绑定错误,而是使用了转换器。不确定为什么这会导致最初描述的行为,但确实如此。因此,我最初改回使用 Validation.Errors[0] 并且错误消息已正确更新。但是,这会导致返回绑定错误。相反,我在这里实现了建议防止绑定错误 问题已解决。
我的 ControlTemplate 现在看起来像这样:
但是,以前必填字段在加载时被标记为错误,但现在不会再发生这种情况。仅当由于用户输入而发生另一个错误时才会出现验证。我该如何解决这个问题?
Ok, so I've sort of fixed this. Rather than use the default implementation that uses Validation.Errors[0] which gives binding errors when the error is fixed, I'd used a converter. Not sure why this resulted in the behaviour originally described, but it did. So I originally changed back to using Validation.Errors[0] and the error message updated correctly. However, this would result in the binding errors returning. Instead I implemented the advice here prevent binding errors and the issue is resolved.
My ControlTemplate now looks like this:
However, where previously a required field was flagged as an error on loading, this no longer happens. The validation only appears once another error has occurred due to user input. How can I fix this?