如何为列表视图中的所选项目启用键盘焦点?

问题描述 投票:0回答:1

我有一个带有列表视图的 winui/uwp 应用程序,当我使用键盘在项目之间导航时,所选项目周围有一个白色边框,如何默认为所选项目启用此边框?所以我不需要使用键盘。我不想在我的 ItemTemplate 中使用 BorderThickness/BorderBrush。我只想默认启用键盘焦点

listview uwp focus winui
1个回答
0
投票

命名您的

ListView
,获取所选项目容器,然后设置 Focus 应该可以工作。

public MainPage()
{
    this.InitializeComponent();
    this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    int selectedIndex = this.ListViewControl.SelectedIndex;

    if (this.ListViewControl.ContainerFromIndex(selectedIndex) is UIElement selectedElement)
    {
        selectedElement.Focus(FocusState.Programmatic);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.