WPF 数据网格无效指示器未清除有效输入
我有一个 DataGrid,它显示了一些东西。每个单元格都使用 IDataErrorInfo 接口进行验证。当用户将无效数据放入单元格时,验证会失败,并且单元格的背景会变成红色,并且行左侧会出现错误图标,从而警告用户。当用户更正数据时,单元格的背景恢复正常,但错误图标不会清除。即使我没有定义 RowValidationErrorTemplate,也会发生这种情况。
<DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Top" Width="Auto"
ItemsSource="{Binding Path=Records}" ColumnWidth="Auto" CanUserReorderColumns="False"
CanUserResizeColumns="False" SelectionUnit="Cell" CanUserAddRows="True">
<DataGrid.RowValidationRules>
<vm:TimeoutRecordRowValidation ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid ToolTip="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type DataGridRow}},
Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
<DataGrid.Resources>
<Style x:Key="DefaultColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Column.(ToolTipService.ToolTip)}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="LabelFamilyID
XXX"
Binding="{Binding Path=LabelFamilyID, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
<DataGridTextColumn Header="ModelCapacityString
WD????????"
Binding="{Binding Path=ModelCapacityString, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
<DataGridTextColumn Header="Timeout Value
99:99:99"
Binding="{Binding Path=TimeoutValue, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
为了简洁起见,我删除了一些不相关的 XAML,例如 HeaderStyle 定义。
I have a DataGrid, which is showing some stuff. Each of the cells has a validation on it, using the IDataErrorInfo interface. When the user puts invalid data into the cell, the validation fails and the user is alerted, both by the cell's background turning red, and the error icon appearing to the left of the row. When the user corrects the data, the cell's background goes back to normal, but the error icon doesn't clear. This happens even if I don't define the RowValidationErrorTemplate.
<DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Top" Width="Auto"
ItemsSource="{Binding Path=Records}" ColumnWidth="Auto" CanUserReorderColumns="False"
CanUserResizeColumns="False" SelectionUnit="Cell" CanUserAddRows="True">
<DataGrid.RowValidationRules>
<vm:TimeoutRecordRowValidation ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid ToolTip="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type DataGridRow}},
Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
<DataGrid.Resources>
<Style x:Key="DefaultColumnHeaderStyle" TargetType="DataGridColumnHeader">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Column.(ToolTipService.ToolTip)}" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="LabelFamilyID
XXX"
Binding="{Binding Path=LabelFamilyID, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
<DataGridTextColumn Header="ModelCapacityString
WD????????"
Binding="{Binding Path=ModelCapacityString, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
<DataGridTextColumn Header="Timeout Value
99:99:99"
Binding="{Binding Path=TimeoutValue, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
ElementStyle="{StaticResource ErrorStyle}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
I have trimmed some of the XAML that is unrelated, like the HeaderStyle definitions, for brevity.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我直接在 DataGrid 下添加了以下内容。它删除了感叹号。这不是“修复”,但也不会产生意想不到的结果
I added the following directly under DataGrid. It removes the exclamation mark. It is not a "fix" but it does not have unexpected results either
我不确定它是什么,但在每个 DataGridTextColumns 中,
从每个 Binding 元素中删除 就可以了。
I'm not sure what it was, but in each of the DataGridTextColumns, removing the
from each of the Binding elements did the trick.
这也删除了感叹号:
我希望 MS 在 VS2013 update 5 的最终版本中纠正它
this remove the exclamation mark too:
I hope MS correct it in the final version of VS2013 update 5