【发布时间】:2017-01-27 20:34:01
【问题描述】:
我对 WPF 还很陌生,但在过去的几天里,我已经阅读了很多关于它以及 MVVM 的内容。 我的 WPF 显示一个带有自定义列模板的 DataGrid(使用 Xceed WPF 工具包中的 NumericUpDown 控件)。其中三列包含 3D 矢量的十进制坐标。我使用 IDataErrorInfo 来确保向量的长度永远不会为 0(所有三列不能同时为 0)。到目前为止,这一切正常,当验证失败时,单元格被标记为红色,但我还想在工具提示或类似内容中显示错误消息。
<DataGrid [...]>
<DataGrid.Columns>
[...]
<DataGridTemplateColumn Header="X" [...]>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<xctk:DecimalUpDown Value="{Binding PositionX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
</xctk:DecimalUpDown>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
[... and same thing for Y and Z]
</DataGrid.Columns>
</DataGrid>
这是我现在被困几个小时的地方,所以我希望你能在这里帮助我:
如何在自定义模板列上显示错误工具提示?
我已经阅读了很多关于错误工具提示的文章和线程,但其中大部分都在纯 TextBox 或 DataGridTextColumns 上,并且尝试了很多,但到目前为止仍无法正常工作。
它们中的大多数看起来像这样:
<Style x:Key="errorStyle" TargetType="{x:Type TextBox}">
<Setter Property="Padding" Value="-2"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
从这里: https://msdn.microsoft.com/library/ee622975%28v=vs.100%29.aspx
或更多示例:
- Display validation error in DataGridCell tooltip
- https://stackoverflow.com/a/7900510/5025424
- https://harishasanblog.blogspot.de/2011/01/datagrid-validation-using.html
- WPF data validation and display error message IDataErrorInfo and error templates
没有任何东西向我显示任何工具提示。
你能给我一个提示吗,
- 如何此样式触发器定义必须查找不包含 TextBox 的单元格,
- 在哪里定义必须在
- 如果列需要以某种方式引用此定义?
谢谢!
【问题讨论】:
标签: c# wpf validation datagrid idataerrorinfo