Warn about CapsLock

capslock, wpf, wpfdatagrid

Solution

You could show a ToolTip

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

Problem

I have a DataGridTemplateColumn with DataTemplate as a PasswordBox. 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. 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.

Original source