T Jasinski
T Jasinski

Reputation: 121

Resetting control tooltip to "default" set tooltip

I have a form filled with multiple text fields that need to filled out by a user. Each text field has a tooltip to help the user with an example of the expected values for the text field.

<!-- Handle xaml-->
<Label x:Name="lbmyHandle" Content="Handle:" HorizontalAlignment="Left" Margin="69,80,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtmyHandle" HorizontalAlignment="Left" Height="23" Margin="123,84,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" ToolTip="Example: [Machine brand].[Machine model].[Machine s/n]"/>

After the user clicks a button I validate each text field to check if anything has been filled out and if what has been filled out matches expected values. If this is not the case I set the tooltip of the text field to indicate that the user has to change the value of the text field.

Now the question, when the user clicks the button again after editing the values that were indicated as wrong I want to reset the tooltip of the text fields back to the "default" tooltip ( if they were corrected ). Meaning the tooltip that was set in the XAML. Is this value stored somewhere, or do I need to store this manually, and if so what would be the best practice to store these values.

Upvotes: 0

Views: 334

Answers (2)

mm8
mm8

Reputation: 169200

Is this value stored somewhere, or do I need to store this manually, and if so what would be the best practice to store these values

When set it like this, it's only stored in the ToolTip property of the TextBox named "txtmyHandle":

<TextBox x:Name="txtmyHandle" ... ToolTip="Example: [Machine brand].[Machine model].[Machine s/n]"/>

If you set this property to another value at a later stage, the original string value is gone unless you store it somewhere.

You could for example do this just before you set the property to another value in your code, e.g.:

string orginalToolTipValue = txtmyHandle.ToolTip?.ToString();
txtmyHandle.ToolTip = "something else..";

orginalToolTipValue may be a field of your class.

Upvotes: 0

StayOnTarget
StayOnTarget

Reputation: 13008

One approach would be to declare the default tooltip text as a string in a resource, and then you could set it with something like:

... ToolTip="{StaticResources defaultTooltipTextKey}" ...

Or similarly you could set the text in a C# class and access it like:

... ToolTip="{x:Static LocalNamespace:ToolTips.ToolTip1}" ...

where the class would look like:

LocalNamespace
{
   public static class ToolTips
   {
        public static string ToolTip1 { get; } = "Example: [Machine brand]...";
   }
}

With either approach you would be able to refer back to the original string later on. The second one would be easier to do so from C# code.

Upvotes: 1

Related Questions