John
John

Reputation: 307

WPF: Show Tooltip message on custom DataGrid cells when IDataErrorInfo

I'm pretty new to WPF but I've read a lot on it as well as MVVM in the last couple if days. My WPF displays a DataGrid with custom column templates (using the NumericUpDown Controls from Xceed WPF Toolkit). Three of the columns contain the decimal coordinates of a 3D vector. I use IDataErrorInfo to make sure the vector's length is never 0 (all three columns cannot be 0 at the same time). This is working fine so far, the cells are marked red when the validation fails, but I also want to show the error message in a tooltip or similar.

<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>

This is where I'm stuck for a few hours now, so I hope you can help me here:

How can I show a error tooltip on custom template columns?

I've read through a lot of articles and threads on error tooltips but most of them are on plain TextBox or DataGridTextColumns and a tried a lot but could not make it working so far.

Most of them look something like this:

<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>

from here: https://msdn.microsoft.com/library/ee622975%28v=vs.100%29.aspx

or more exsamples:

Nothing on this ever showed any Tooltip to me.

Can you give me a hint,

Thank you!

Upvotes: 2

Views: 1200

Answers (1)

mm8
mm8

Reputation: 169160

Set the Style property of the control to a Style with a trigger that sets the Tooltip property of the control in the CellTemplate if the attached Validation.HasError property returns true:

<DataGridTemplateColumn Header="X">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <xctk:DecimalUpDown Value="{Binding PositionX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
                <xctk:DecimalUpDown.Style>
                    <Style TargetType="xctk:DecimalUpDown">
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="True">
                                <Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </xctk:DecimalUpDown.Style>
            </xctk:DecimalUpDown>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Upvotes: 2

Related Questions