user380719
user380719

Reputation: 9903

How can I redirect the focus in WPF?

How can I set an element to be the default focus of other elements?

For example, say I have the following:

<StackPanel><Label/><Button/></StackPanel>

Clicking on any element will give it the focus if the Focusable is true.

However, what I need to say is "if the user clicks anywhere in the stackpanel, the button should get the focus". In other words, clicking on the label will give the focus to the button. This should work in small samples such as this one but also much larger ones with control templates.

Is this possible?

Upvotes: 0

Views: 691

Answers (2)

Nick Heidke
Nick Heidke

Reputation: 2847

What about something like:

private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
    {
        <elementName>.Focus();
    }

Upvotes: 3

Bala R
Bala R

Reputation: 108937

StackPanel has GotFocus event. In the event handler for that event, give button the focus ( by calling button1.Focus()).

Upvotes: 2

Related Questions