如何将焦点设置到 UWP 用户控件内的元素?

How to set focus to an element inside of user control on UWP?

我创建了自己的UserControl,在里面,我放了一个ListView。但是当我设置 UserControl 显示时,我无法将焦点设置到 ListViewItem。焦点还在原来的页面。

关于如何显示我的UserControl,我使用Popup

Popup popup = new Popup();
popup.Child = this;
popup.IsOpen = true;

ListView 已加载到可视化树中并准备好进行交互时,就会为 ListView 设置焦点。

我们假设您已经创建了一个包含 ListViewUserControl,那么为 ListView 设置 Focus 的方法应该在 中完成UserControl_LoadedListView_Loaded 事件。

public PopupControl()
{
    this.InitializeComponent();
    // Data init
    var _popup = new Popup();
    _popup.Child = this;
    _popup.IsOpen = true;
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    TestListView.Focus(FocusState.Keyboard);
}

此致。