本文介绍了警告CapsLock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridTemplateColumn,其中DataTemplate作为PasswordBox。

I have a DataGridTemplateColumn with DataTemplate as a PasswordBox.

如果要切换CapsLock,我想警告用户。

I want to warn user if CapsLock is toggled.

private void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
    {
        if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled)
        {
         ...

现在,我需要在此处提出一些弹出窗口。我不知道该怎么做。

Now, I need to raise some PopUp here. I don't know how to do this. Help me please.

我试图像这样玩ToolTip:

I tried to play around with ToolTip like this:

((PasswordBox)sender).SetValue(ToolTipService.InitialShowDelayProperty, 1);
((PasswordBox)sender).ToolTip = "CAPS LOCK";

但是只有当鼠标指针悬停在那儿并且我需要一个独立的弹出窗口时,它才起作用。

But it works only when mouse cursor hovers there and I need an independent Popup.

推荐答案

您可以显示工具提示

private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
    if ((Keyboard.GetKeyStates(Key.CapsLock) & KeyStates.Toggled) == KeyStates.Toggled)
    {
        if (PasswordBox.ToolTip == null)
        {
            ToolTip tt = new ToolTip();
            tt.Content = "Warning: CapsLock is on";
            tt.PlacementTarget = sender as UIElement;
            tt.Placement = PlacementMode.Bottom;
            PasswordBox.ToolTip = tt;
            tt.IsOpen = true;
        }
    }
    else
    {
        var currentToolTip = PasswordBox.ToolTip as ToolTip;
        if (currentToolTip != null)
        {
            currentToolTip.IsOpen = false;
        }

        PasswordBox.ToolTip = null;
    }
}

这篇关于警告CapsLock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 11:03