开发者

How can I redirect the focus in WPF?

How can I set an element to be the开发者_JAVA技巧 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?


What about something like:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜