I'm building a login screen in 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With