如何设置对gridview项目的编程重点?

时间:2019-06-03 10:31:51

标签: c# xaml uwp uwp-xaml

我的主页有一个ListView,其中包含许多GridView(已启用一行)。 现在,我可以使用键盘left/right/up/down来设置焦点。但是,当输入一个项目(我使用全局变量存储该项目),导航到另一页并向后导航时,我无法设置上一个GridView项的焦点。

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back)
    {
        //Set last focus, if no focus, banner get the focus.              
        if (Globals.gridViewItemFocused == null)
            banner.Focus(FocusState.Pointer);
        else
        {
            list.UpdateLayout();

            Globals.gridViewItemFocused.Focus(FocusState.Programmatic);
            Globals.gridViewItemFocused.IsSelected = true;
            Globals.gridViewItemFocused.StartBringIntoView();
        }
    }

    base.OnNavigatedTo(e);
}

enter image description here

1 个答案:

答案 0 :(得分:1)

将焦点设置为GridViewItem类型的变量将不起作用。您需要根据变量设置GridView控件的SelectedItem或SelectedIndex。