在我的WPF应用程序(.Net 4.5)中,我想向UI提供有关验证结果的扩展视觉反馈。数据层的验证引擎通过INotifyDataErrorInfo接口返回警告和错误。
根据错误类型和错误消息列表,我有以下XAML显示红色或橙色边框。这里errorToColor
是值转换器的资源键,如果Validation.Errors
集合中存在至少一个错误,并且只有警告存在橙色刷,则返回红色刷。在Validation.Errors绑定中使用转换器
<TextBox Name="MappingName" Text="{Binding Path=Mapping.Name, NotifyOnValidationError=True}" >
<Validation.ErrorTemplate>
<ControlTemplate>
<DockPanel>
<Border BorderBrush="{Binding Converter={StaticResource errorsToColor}}" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
<ListView DisplayMemberPath="ErrorContent" ItemsSource="{Binding}" />
</DockPanel>
</ControlTemplate>
</Validation.ErrorTemplate>
</TextBox>
现在让我们来看看当我在TextBox中键入一些“无效”文本时会发生什么。
- 键入'Text1'并更改焦点。
调试器进入转换器,两个验证器导致ListView中的两个项目(1个错误和1个警告)和一个红色边框。 [确定] - 键入'文本'来纠正错误,改变焦点。
价值转换器甚至没有被击中!当然,同样的红色边框。但是ListView已经改变并且只显示一个警告。
有人可以解释发生了什么吗?为什么ListView收到收集更改通知和边界不是?这是因为ListView
是ItemsControl
而Validation.Errors
是否包含在CollectionView
?
它可能与http://stackoverflow.com/questions/2816163/when-will-the-valueconverters-convert-method-be-called-in-wpf 添加新项目Validation.Errors引发CollectionChanged事件而PropertyChanged触发值转换器 – yuvin