Question

Here is my code.

private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            Boolean Capslock = Console.CapsLock;
            if (Capslock == true)
            {
                txtPassword.ToolTip = "Caps Lock is On.";
            }
        }

I'm trying to get a tooltip to show on TextChanged Event on WPF Control. The above code works fine and shows the tooltip with the above text when I move my mouse over the txtPassword control if Caps Lock is on.

But I'm looking for something that will show the tooltip when you start typing regardless of mouse over txtPassword Control or not. Like when the txtPassword Control is focused or something similar

Any help will be appreciated.

Was it helpful?

Solution

You might want to consider using a PopUp for this.

XAML:

<TextBox x:Name="txtPassword" Height="30" Width="100" TextChanged="txtPassword_TextChanged" ></TextBox>
<Popup x:Name="txtPasswordPopup" Placement="Top" PlacementTarget="{Binding ElementName=txtPassword}" IsOpen="False">
    <TextBlock x:Name="PopupTextBlock" Background="Wheat">CAPSLOCK IS ON!</TextBlock>
</Popup>

Code-Behind:

private void txtPassword_TextChanged(object sender, TextChangedEventArgs e)
    {
        Boolean Capslock = Console.CapsLock;
        if (Capslock == true)
        {
            PopupTextBlock.Text = "Caps Lock is On.";
            txtPasswordPopup.IsOpen = true;
        }
        else
        {
            txtPasswordPopup.IsOpen = false;
        }
    }

OTHER TIPS

you need to use a tooltip control and set StaysOpen and IsOpen properties to true, this caueses the tooltip to stay open till you will close it by IsOpen =false (maybe on lostFocus) here is the code:

 private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
         Boolean Capslock = Console.CapsLock;
         if (Capslock == true)
         {
             ToolTip toolTip = new ToolTip();
             toolTip.Content = "Caps lock is on";
             toolTip.StaysOpen = true;
             toolTip.IsOpen = true;

             (sender as TextBox).ToolTip = toolTip;
         }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top