Is there any way to set a tooltip inside an ErrorTemplate?

1.7k Views Asked by At

In our WPF application we have a common control template which we use to display errors in a consistent way

<ResourceDictionary>
    <ControlTemplate x:Key="ErrorTemplate">
        <Border BorderThickness="1" BorderBrush="Red">
            <AdornedElementPlaceholder />
        </Border>
    </ControlTemplate>
</ResourceDictionary>

Elsewhere in our application when a control might display an error we set the ErrorTemplate like so

<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />

I now want to display a tool tip in this error template, however setting the tooltip property on the border doesn't help that much as the tooltip only displays when the user mouses over the 1px wide border, not the control itself which is in error.

I know that I can set the tooltip in a style, however this error template is applied to many different controls (combo boxes etc...) and many of these controls also use styles which are independent from my error template - I really want to be able to apply my error template in a generic way to any control.

Is there any way that I can set a tooltip in my ErrorTemplate?

3

There are 3 best solutions below

1
On

I have a style defined. I have IDataErrorInfo on my object (Customer) which does the validation for the property(LastName) which is databound to a text box for example. Here's my style:

<Style x:Key="ValidationTextBox" TargetType="{x:Type Control}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="0,2,40,2"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Border Background="#B22222" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White"/>
                            </Border>
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                                <Border BorderBrush="#B22222" BorderThickness="1" />
                            </AdornedElementPlaceholder>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style

<TextBox Style="{StaticResource ValidationTextBox}" Text="{Binding Path=Customer.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

1
On

As I stated in my answer here you can:

<ControlTemplate x:Key="ErrorTemplate">
    <Border BorderThickness="1" BorderBrush="Red"
            Background="Transparent"
            ToolTip="{Binding Path=/ErrorContent}">
        <AdornedElementPlaceholder />
    </Border>
</ControlTemplate>
0
On

I'm sorry I didn't have time yesterday...Would you try below and see if this is what you are after, please?

<Style x:Key="ValidationTextBox2" TargetType="{x:Type Control}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <DockPanel LastChildFill="True" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Background="Transparent">
                                <TextBlock />
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                            </AdornedElementPlaceholder>
                        </DockPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>