Wednesday, March 24, 2010

How to Create ToolTips on the cheap – integrating ToolTip and IDataErrorInfo.

In a previous post I discussed how to create a more pleasing to the eye error template.

But it has been noted by the WPF guru Karl Shifflett and others that this simple mechanism for integrating tooltips and IDataErrorInfo has some draw backs. The most annoying of which is that you can’t assign a tool tip to any control that might show a data validation error! If you do assign a tooltip, the data validation error will not appear.

There is a simple work around. Use the tag of the control to hold the tooltip and use it for the content of the tooltip when there is no data validation error. Here are a few screen shots of the tooltips and data validation in action.

Here is the relevant code snippet:

<Trigger Property="Validation.HasError" Value="True">
In the following xaml, Path=AdornedElement.(Validation.Errors)[0].ErrorContent} throws an exception if no error condition is present,
although the exception does not cause a crash. It shows up in the debug window which is annoying.
This work around implemented here -
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
<Trigger Property="Validation.HasError" Value="False">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" />

The tooltip is set using the tag of the control as content in the second trigger when Validation.HasErrors is false. Note that I implemented Josh Smith's fix for debug spew.

The full code is available here. 

Todd Stephan

No comments:

Post a Comment