views:

113

answers:

0

Hi,

I have a WPF UserControl (B) as a child of another UserControl (A). I have a situation where in some instances B requests focus but A actually gets the focus. Does anyone have any idea why this might be happening?

Code in B to get the focus:

log.Debug("Asked for focus.");

//We expect the user to do a button press and therefore need focus
Focusable = true;
Keyboard.Focus(this);

Code in A for detecting when it gets focus:

protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
    base.OnGotKeyboardFocus(e);

    log.Warn("Should not have received focus!");
}

I see the log line 'Asked for focus' in the logs followed by 'Should not have received focus!'

Any help is appreciated.

James