0

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.

T Jasinski
  • 113
  • 2
  • 12
  • 1
    You are doing *validation* ([related](https://stackoverflow.com/q/19539492/1997232)) and in WPF validation errors are displayed using *adorner* layer. In this layer you can have a nice error indicator (icon, red border, etc.) with tooltip, but please don't change tooltip of control itself. – Sinatr Sep 10 '20 at 09:24
  • Thank you @Sinatr , if you want you can type your comment into an answer and I'll accept it. – T Jasinski Sep 10 '20 at 10:01
  • 1
    My comment is barely a hint. Feel free to turn it into a good [self-answer](https://stackoverflow.com/help/self-answer) for future readers. – Sinatr Sep 10 '20 at 10:14

2 Answers2

1

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
0

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.

mm8
  • 163,881
  • 10
  • 57
  • 88