views:

394

answers:

2

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.

A: 

It completely depends on your architecture, but for a simple solution:

You should be setting a Dependency Property, which will be observed by some sort of control on the Window that will become visible and will display the warning for the user.

Chris Nicol
+2  A: 

You could show a ToolTip

private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
    if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled)
    {  
        ToolTip tt=new ToolTip();
        tt.Content="InValid";
        tt.Placement = PlacementMode.Custom;
        PasswordBox.ToolTip=tt;
        tt.IsOpen = true;
    }
    else
    {
        Passwordbox.ToolTip=null;
    }
}
Patrick McDonald
Oh... it seems this one goes for me, but it still opens the ToolTip at the mouse cursor. I need a ToolTip right under the PasswordBox
Ike
tt.Placement = PlacementMode.Custom;Now it's under the PasswordBox
Ike
thanks plotnick
Patrick McDonald
Patrick... I have another problem. ToolTip doesn't go anywhere. Even if PasswordBox loses control. How to force it to fade-off?I've tried to kill it in LostFocus event, but that thing you showed me gives birth every time to new ToolTip...
Ike