.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > Validation.ErrorTemplate: override ToolTip style
 

Validation.ErrorTemplate: override ToolTip style

Hi folks,

I have a ValidationErrorTemplate that looks like this:

    <Style x:Key="ErrorTemplate" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True" Margin="3">
                        <!-- Put an asterisk-character (white on red) next to the control in question -->
                        <TextBlock DockPanel.Dock="Right" Foreground="White" FontSize="14" FontWeight="Bold" Background="Red" Padding="3" Text="!"
                                ToolTip="{Binding ElementName=Control_AdornedElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                        </TextBlock>
                        <!-- Draw a red border around the control -->
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder Name="Control_AdornedElementPlaceholder"/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <!-- Set the tooltip to the error message if there is one -->
            <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>

    <!-- All controls that are to use the ErrorTemplate need to be added here -->
    <Style TargetType="TextBox" BasedOn="{StaticResource ErrorTemplate}" />
    <Style TargetType="CheckBox" BasedOn="{StaticResource ErrorTemplate}" />
    <Style TargetType="ComboBox" BasedOn="{StaticResource ErrorTemplate}" />


I would now like to define a ToolTip-style that is being used for the above ErrorTemplate only.
At present, the default ToolTip style applies and I cannot find the right way to apply my own style, i.e. with white text on red.

Any pointers appreciated.

Cheers & TIA,
Olaf
Olaf_R
Hi,

forget what I wrote earlier. I just found out that the DataContext of the ErrorTemplate is the Validation.Errors collection of the validated control. So, if you want the text of the error message in your error template, you can use {Binding Path=/ErrorContent} .
  • Marked As Answer byOlaf_R Friday, September 25, 2009 6:51 AM
  •  
hbarck
Hi folks,

since nobody answered, I'll try to rephrase what I'd like to do:

I'm changing the appearance of (databound) controls that contain a validation error - a red border around them is added and a textblock to their right that contains a white exclamation mark on a red background. Th "!"-textblock's tooltip is then containing the error message.
I'd now like to change the appearance of the validation error tooltip, without influencing the way "regular" tooltips are being displayed. What I tried to do is to change the Tooltip being displayed in my ErrorValidation template directly, like:

    <Style x:Key="ErrorValidationTooltip" TargetType="{x:Type ToolTip}">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>

    <Style x:Key="ErrorTemplate" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True" Margin="3">
                        <TextBlock DockPanel.Dock="Right" Foreground="White" FontSize="14" FontWeight="Bold" Background="Red" Padding="3">
                            <TextBlock.Text>!</TextBlock.Text>  
                            <TextBlock.ToolTip>
                                <ToolTip Style="{StaticResource ErrorValidationTooltip}" >
                                    <TextBlock Text="{Binding Path=AdornedElement.ToolTip, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Adorner}}}"/>
                                </ToolTip>
                            </TextBlock.ToolTip>
                        </TextBlock>
                        <Border BorderBrush="Red" BorderThickness="1">
                            <AdornedElementPlaceholder Name="Control_AdornedElementPlaceholder"/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <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>


But with the above XAML, the inner Textblock cannot find the error message:

Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Documents.Adorner', AncestorLevel='1''. BindingExpression:Path=AdornedElement.ToolTip; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

So it seems that all I really need is to change the binding of the TextBlock contained in the tooltip (unless there's a better way of doing this).
Any ideas?

Cheers,
Olaf
Olaf_R
Hi,

probably youcanjust use {TemplateBinding Path=(Validation.Errors).CurrentItem.ErrorContent} instead of the binding expression that causes the error, and delete the trigger that sets the ToolTip on the original control. Since you are extending the ControlTemplate of the control that shows the error, it's Validation.Errors attached property should be accessible through a TemplateBinding. However, I haven't tested this...
hbarck
Hi hbarck,

probably youcanjust use {TemplateBinding Path=(Validation.Errors).CurrentItem.ErrorContent} instead of the binding expression that causes the error, and delete the trigger that sets the ToolTip on the original control. Since you are extending the ControlTemplate of the control that shows the error, it's Validation.Errors attached property should be accessible through a TemplateBinding. However, I haven't tested this...

nope, didn't find a way of making it work that way - with "Path=" I get a design time error and without I'll get a BindingExpression error with that (i.e. with Text="{TemplateBinding (Validation.Errors).CurrentItem.ErrorContent}"). :-(

Any other ideas? It can't really be all that hard, can it?
No hair to be pulled out left here ... :-)

Cheers,
Olaf
Olaf_R
Hi,

forget what I wrote earlier. I just found out that the DataContext of the ErrorTemplate is the Validation.Errors collection of the validated control. So, if you want the text of the error message in your error template, you can use {Binding Path=/ErrorContent} .
  • Marked As Answer byOlaf_R Friday, September 25, 2009 6:51 AM
  •  
hbarck
Hi hbarck,

perfect! Many thanks - one problem less!

Cheers,
Olaf
Olaf_R

You can use google to search for other answers

Custom Search

More Threads

• MVVM for including user control
• "like window" control?
• HierarchicalDataTemplate & Treeview
• TemplateBinding a Styled ComboBoxItem to Inherit the Height of the ComboBoxToggleButton
• Listview tabindex not working
• Binding a WPF listview to a DataTable
• Changing DataTemplate's TextBlock Foreground based on DataTrigger
• Creating a popup near the clock
• window border is white on top right side and dark on bottom left.
• Accessing the resources of the current application from a library