简体   繁体   English

C#:专注于WPF文本框的工具提示

[英]C#: Tooltip on Focus on WPF TextBox

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. 我正在尝试获取在WPF控件上的TextChanged事件上显示的工具提示。 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. 如果将Caps Lock打开,则将鼠标移到txtPassword控件上时,上面的代码可以正常工作,并显示带有上述文本的工具提示。

But I'm looking for something that will show the tooltip when you start typing regardless of mouse over txtPassword Control or not. 但是我正在寻找可以在您开始键入时显示工具提示的内容,无论是否将鼠标悬停在txtPassword Control上。 Like when the txtPassword Control is focused or something similar 就像当txtPassword控件处于焦点状态或类似情况时

Any help will be appreciated. 任何帮助将不胜感激。

You might want to consider using a PopUp for this. 您可能要考虑为此使用PopUp

XAML: 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;
        }
    }

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: 您需要使用工具提示控件,并将StaysOpen和IsOpen属性设置为true,这会导致工具提示保持打开状态,直到您通过IsOpen = false(可能在lostFocus上)将其关闭为止,这是代码:

 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;
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM