在DataGridCell工具提示中显示验证错误

时间:2022-08-26 23:35:36

I have a WPF DataGrid which displays types that implement IDataErrorInfo. As expected when the validation fails the row gets the red exclamation mark and the invalid cell gets the red highlight. 在DataGridCell工具提示中显示验证错误

我有一个WPF DataGrid,它显示实现IDataErrorInfo的类型。正如预期的那样,当验证失败时,行会获得红色感叹号,无效的单元格会获得红色突出显示。

This is all well and good; however, I want the validation error message to display in the tooltip of the invalid cell so the user has some indication of what is wrong. I presently have:

这一切都很好;但是,我希望验证错误消息显示在无效单元格的工具提示中,以便用户有一些错误的指示。我现在有:

<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                                                Path=(Validation.Errors[0].ErrorContent}"/>
     </Style>
</DataGrid.CellStyle>

This approach works for TextBox but not for DataGridCell. What is the difference?

此方法适用于TextBox,但不适用于DataGridCell。有什么不同?

2 个解决方案

#1


7  

I have something similiar in a project I'm working on right now, and it goes something like this:

我现在正在研究的项目中有类似的东西,它是这样的:

<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="DataGridCell.ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                                                Path=(Validation.Errors)[0].ErrorContent}"/>
     </Style>
</DataGridTextColumn.ElementStyle>

#2


2  

Take a look at this MSDN log post:

看看这篇MSDN日志帖子:

https://blogs.msdn.microsoft.com/bethmassi/2008/06/27/displaying-data-validation-messages-in-wpf/

Follow its instructions to create a textbox cell editing template that will look something like this:

按照其说明创建一个文本框单元格编辑模板,如下所示:

<Style TargetType="TextBox" x:Key="errTemplate">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
              Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                     Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

Then, you can use it in your datagrid by setting the EditingElementStyle like so:

然后,您可以通过设置EditingElementStyle在您的数据网格中使用它,如下所示:

<DataGridTextColumn Header="Variable" 
                    Binding="{Binding Variable, ValidatesOnDataErrors=True}" 
                    EditingElementStyle="{StaticResource errTemplate}"/>

It is important to use the data trigger so that you can support a standard tool tip as well as a tool tip when there is an error as explained in this post:

使用数据触发器非常重要,这样当出现错误时,您可以支持标准工具提示和工具提示,如本文所述:

Tooltip Not Showing Up When No Validation Error WPF

没有验证错误WPF时,工具提示不显示

#1


7  

I have something similiar in a project I'm working on right now, and it goes something like this:

我现在正在研究的项目中有类似的东西,它是这样的:

<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="DataGridCell.ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                                                Path=(Validation.Errors)[0].ErrorContent}"/>
     </Style>
</DataGridTextColumn.ElementStyle>

#2


2  

Take a look at this MSDN log post:

看看这篇MSDN日志帖子:

https://blogs.msdn.microsoft.com/bethmassi/2008/06/27/displaying-data-validation-messages-in-wpf/

Follow its instructions to create a textbox cell editing template that will look something like this:

按照其说明创建一个文本框单元格编辑模板,如下所示:

<Style TargetType="TextBox" x:Key="errTemplate">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
              Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                     Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

Then, you can use it in your datagrid by setting the EditingElementStyle like so:

然后,您可以通过设置EditingElementStyle在您的数据网格中使用它,如下所示:

<DataGridTextColumn Header="Variable" 
                    Binding="{Binding Variable, ValidatesOnDataErrors=True}" 
                    EditingElementStyle="{StaticResource errTemplate}"/>

It is important to use the data trigger so that you can support a standard tool tip as well as a tool tip when there is an error as explained in this post:

使用数据触发器非常重要,这样当出现错误时,您可以支持标准工具提示和工具提示,如本文所述:

Tooltip Not Showing Up When No Validation Error WPF

没有验证错误WPF时,工具提示不显示