uwp xaml declaratively defining control that has focus by default

独自空忆成欢 提交于 2020-04-30 06:37:09

问题


I've been able to set focus on my view's Close button using the following code in the MainPage.xaml.cs OnNavigatedTo() event handler.

this.close.Focus(FocusState.Programmatic);` 
// or 
Windows.UI.Xaml.Input.FocusManager.TryFocusAsync(this.Close, FocusState.Programmatic);`

I'd prefer a way to declaratively set this in the xaml markup. Is there a way to do that?

I've tried following samples provided in SO post hits on this subject showing approaches llike the following using FocussedElement, which appears to have been deprecated, and am having no luck.

<Page . . . xmlns:xi="using:Windows.UI.Xaml.Input">
    <StackPanel . . . FocusManager.FocusedElement="{Binding ElementName=Close}">
    <!-- or -->
    <Button Name="Close" . . . xi:FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">Close</Button>

来源:https://stackoverflow.com/questions/61349354/uwp-xaml-declaratively-defining-control-that-has-focus-by-default

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!