如何在DataGridTextColumn上的工具提示中显示IDataErrorInfo的错误?

时间:2011-11-08 04:12:07

标签: wpf datagrid

使用textbox,当我将以下xml放在App.xml中时,它运行良好:

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

但它只适用于文本框,如何使用DataGridTextColumn做类似的事情?

1 个答案:

答案 0 :(得分:14)

只需使用DataGridTextColumn.ElementStyle和/或DataGridTextColumn.EditingElementStyle,例如:

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