I want to use tooltips as they were intended. But when an error occurs, I want to change them to show the error message, then, when the error is fixed, change them back.
So I have created an attached property to hold the tooltip. I assign the tooltip to the attached property and then use a style to copy that to the tooltip property. If there is an error, the style sets the tooltip to the error message instead.
So the triggers to set the error message in the tooltip are:
<Trigger Property="Validation.HasError"
Value="true">
<Setter Property="BorderBrush"
Value="{DynamicResource controls-errorBorderBrush}" />
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
That seems fairly easy (and works)
When the error is fixed, I set it back (this doesn't work):
<Trigger Property="Validation.HasError"
Value="false">
<Setter Property="ToolTip"
Value="{Binding Path=(wpfMisc:myCtrl.tooltipValue)}" />
</Trigger>
And in the xaml file I have:
<TextBox Text="this is a textbox with a myMisc based tooltip"
Name="txtTooltip2"
wpfMisc:myCtrl.tooltipValue="Tooltip Test tooltip" />
So of course, the problem is most likely in my attached property as it appears that the information is not being saved correctly. Here is that code:
public static string GettooltipValue(DependencyObject obj)
{
string value = obj.GetValue(tooltipValueProperty).ToString() ;
value = value.trimNull() ; // extension method to insure at least an empty string
return value ;
}
public static void SettooltipValue(DependencyObject obj, string value)
{
obj.SetValue(tooltipValueProperty, value.trimNull() );
}
public static readonly DependencyProperty tooltipValueProperty =
DependencyProperty.RegisterAttached("tooltipValue",
typeof(string),
typeof(myCtrl),
new UIPropertyMetadata(string.Empty));
So my guess is that I need to use something different in the UIPropertyMetaData, but not sure what I would use. Or is my whole approach just wrong?
I want to have data specific tooltips for all the data fields.
I did have this working by moving the tooltip to the tag property during an error, but I didn't want to leave it working that way because I know I would end up having problems when some other code wanted to use the tag in some special way.
Also, I know that some of the code is verbose - just a side effect of debugging...
And another dependency property in myCtrl is working just fine, so I know the xmlns, etc. references are correct.
On further research, I found the following in the output window: System.Windows.Data Error: 17 : Cannot get 'tooltipValue' value (type 'String') from '' (type 'layoutSettingsViewModel'). BindingExpression:Path=(0); DataItem='layoutSettingsViewModel' (HashCode=46457861); target element is 'TextBox' (Name=''); target property is 'ToolTip' (type 'Object') InvalidCastException:'System.InvalidCastException: Unable to cast object of type 'client.Models.layoutSettings.layoutSettingsViewModel' to type 'System.Windows.DependencyObject'.
layoutSettingsViewModel is the xaml view. So I think that the view itself is somehow getting the value instead of the controls.... Not sure though - I am guessing one of you knows exactly what it means and why... I hate trying to get up to speed on a new language...
Anyway, any help and/or suggestions are appreciated.