WPF DataGridTextColumn Valdiation Error Not Triggering ToolTip

75 Views Asked by At

For this XAML code, I have the Validation method in my ValidationRule class triggering correctly. The ToolTip is appearing when "Validation.HasError" is False, so the ToolTip "Correct Name appears", but when "Validation.HasError" is true the ToolTip does not appear at all.

I'm not sure if it has anything to do with the cursor being in TextMode when the cursor is hovering over the TextBlock. The TextBlock is outlined in red correctly though in the DatGridText cell.

            <DataGridTextColumn x:Name="clEditS_Name"
                                Header="Name (Editable)"
                                TextBlock.TextAlignment="Center"
                                Width="*">
                <DataGridTextColumn.Binding>
                    <Binding Path="S_Name" 
                         NotifyOnTargetUpdated="True" 
                         UpdateSourceTrigger="PropertyChanged" 
                         Mode="TwoWay"
                         ValidatesOnDataErrors="True">
                        <Binding.ValidationRules>
                            <ui:ValidationClass />
                        </Binding.ValidationRules>
                    </Binding>
                </DataGridTextColumn.Binding>
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="True">
                                <Setter Property="ToolTip" Value="Invalid Name"/>
                            </Trigger>
                            <Trigger Property="Validation.HasError" Value="False" >
                                <Setter Property="ToolTip" Value="Correct Name"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

Below is the Validation Method that is triggering correctly.

    public class ValidationClass : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string stringValue = value as string;
        if (!string.IsNullOrEmpty(stringValue))
        {
            if (stringValue.Contains("RR"))
            {
                return new ValidationResult(false, "Issue in Name");
            }
        }

        return new ValidationResult(true, null);
    }
}

ToolTip appearing when Validation HasError is False

ToolTip not appearing when Validation HasError is false

ToolTip not appearing when Validation HasError is True

1

There are 1 best solutions below

2
On BEST ANSWER

I'm not sure if it has anything to do with the cursor being in TextMode when the cursor is hovering over the TextBlock.

When you're in "TextMode", the TextBlock is actually replace by a TextBox, so you should also define an EditingElementStyle:

<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="ToolTip" Value="Invalid Name"/>
            </Trigger>
            <Trigger Property="Validation.HasError" Value="False" >
                <Setter Property="ToolTip" Value="Correct Name"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="ToolTip" Value="Invalid Name"/>
            </Trigger>
            <Trigger Property="Validation.HasError" Value="False" >
                <Setter Property="ToolTip" Value="Correct Name"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</DataGridTextColumn.EditingElementStyle>