简体   繁体   English

绑定到大写锁定

[英]Bind to Caps Lock

I'm building a login screen in WPF. 我正在WPF中构建一个登录屏幕。 I'm trying to figure out how to bind a part of my code to only be visible when the caps lock key is on. 我试图找出如何绑定我的代码的一部分只有在大写锁定键打开时才可见。

<StackPanel Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="1" Orientation="Horizontal">
        <Image Source="../../../Resources/Icons/109_AllAnnotations_Warning_16x16_72.png" Height="16" Width="16"/>
        <Label>Caps lock is on</Label>
</StackPanel>

I would prefer a solution with xaml binding only. 我更喜欢只有xaml绑定的解决方案。

We're using the following approach in our sign in form to show a 'Caps lock warning' when the password box has focus. 我们在登录表单中使用以下方法,以在密码框具有焦点时显示“大写锁定警告”。

    private void PasswordBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        UpdateCapsLockWarning(e.KeyboardDevice);
    }

    private void PasswordBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        UpdateCapsLockWarning(e.KeyboardDevice);
    }

    private void PasswordBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        CapsLockWarning.Visibility = Visibility.Hidden;
    }

    private void UpdateCapsLockWarning(KeyboardDevice keyboard)
    {
        CapsLockWarning.Visibility = keyboard.IsKeyToggled(Key.CapsLock) ? Visibility.Visible : Visibility.Hidden;
    }

Not the binding-only answer you're looking for though. 不是你正在寻找的只有约束力的答案。

Expanding on your xaml: 扩展您的xaml:

<Window x:Class="LoginWindow" 
        ... 
        Activated="Window_Activated"
        PreviewKeyDown="Window_PreviewKeyDown">
    ...
    <StackPanel Name="CapsLockOn" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="1" Orientation="Horizontal">
            <Image Source="../../../Resources/Icons/109_AllAnnotations_Warning_16x16_72.png" Height="16" Width="16"/>
            <Label>Caps lock is on</Label>
    </StackPanel>
    ...
</Window>

Then in code behind: 然后在代码后面:

public partial class LoginWindow : Window
{
    ...

    private void Window_Activated(object sender, EventArgs e)
    {
        SetCapsLockOnState();
    }

    private Window_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        SetCapsLockOnState();
    }

    private void SetCapsLockOnState()
    {
        if (Console.CapsLock)
        {
            CapsLockOn.Visibility.Visible;
        }
        else
        {
            CapsLockOn.Visibility.Hidden;
        }
    }

    ...
}

PreviewKeyDown is a tunneling event . PreviewKeyDown是一个隧道事件 Meaning that the event at the element tree root (ie Window) is called first, before then the event moves along the element tree towards the event source. 这意味着首先调用元素树根(即Window)处的事件,然后事件沿元素树向事件源移动。 This is handy as we can detect changes to caps lock state in one place, without any fear that this may be interfered with by other code. 这很方便,因为我们可以在一个地方检测到大写锁定状态的变化,而不用担心这可能会受到其他代码的干扰。

The PreviewKeyDown event only response to changes in caps lock state. PreviewKeyDown事件仅响应大写锁定状态的更改。 You need to call SetCapsLockState() to correctly set the state of SetCapsLockOnState to reflect the caps lock state when the window is created, and if the caps lock state changes when the window doesn't have focus. 您需要调用SetCapsLockState()以正确设置SetCapsLockOnState的状态以反映创建窗口时的大写锁定状态,以及当窗口没有焦点时大写锁定状态更改。 I have chosen to use the Activated event to cover both of these cases. 我选择使用Activated事件来涵盖这两种情况。

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

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