Let's just display the error message in a ToolTip. Displaying error message in tooltip in error template is a little tricky. For displaying error message in a tooltip we need a workaround on our error template we were using in last section. To achieve this, we have to create a Style in our Window.Resources section that applies to the Textboxes on this form. When the Validation.HasError changes to true, style sets up a Trigger that sets the ToolTip property to the validation message.
<Window.Resources>
<Style TargetType="TextBox">
<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>
</Window.Resources>
Now when we run this again, you'll see that when you hover over the TextBox the validation message is displayed in a ToolTip. But this solution only displays for TextBoxes. What about the rest the controls like Label, CheckBoxes, and Dropdown etc…? To achieve this, we can do something as given below.
<Window.Resources>
<Style TargetType="Control" x:Key="myErrorTemplate">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="12pt" >!!!</TextBlock>
<Border BorderBrush="Green" BorderThickness="1" >
<AdornedElementPlaceholder Name="myControl"/>
</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>
<Style TargetType="TextBox" BasedOn="{StaticResource myErrorTemplate}" />
<!--<Style TargetType="CheckBox" BasedOn="{StaticResource myErrorTemplate}" />-->
</Window.Resources>
Here as the style is taking care of the error display, we don’t have to specify the Validation Error Template as Validation.ErrorTemplate in our applications textbox. We can remove that.
<TextBox Name="textBox1" Text="{Binding TextBoxString, Mode=TwoWay, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}"/>
We can also specify that the TargetType="Control" and then we can declare additional styles for the rest of our controls. Here we are adding the style to window resource, as the style is in the window resource, style will be applicable only to controls in that particular window. If you want to have it across all your windows in your application, try adding this style in your resources section (Application.resources) inside your App.xaml.