如何将焦点设置到 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
设置焦点。
我们假设您已经创建了一个包含 ListView
的 UserControl
,那么为 ListView
设置 Focus 的方法应该在 中完成UserControl_Loaded 或 ListView_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);
}
此致。
我创建了自己的UserControl
,在里面,我放了一个ListView
。但是当我设置 UserControl
显示时,我无法将焦点设置到 ListViewItem
。焦点还在原来的页面。
关于如何显示我的UserControl
,我使用Popup
。
Popup popup = new Popup();
popup.Child = this;
popup.IsOpen = true;
当 ListView
已加载到可视化树中并准备好进行交互时,就会为 ListView
设置焦点。
我们假设您已经创建了一个包含 ListView
的 UserControl
,那么为 ListView
设置 Focus 的方法应该在 中完成UserControl_Loaded 或 ListView_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);
}
此致。