I usually call myControl.Focus()
in the Loaded event handler, but this doesn't seem to work for a ListBox
which is databound to a list of custom objects. When I start my application, the ListBox
's first item is selected but the focus is elsewhere.
我通常在Loaded事件处理程序中调用myControl.Focus(),但这似乎不适用于将数据绑定到自定义对象列表的ListBox。当我启动我的应用程序时,ListBox的第一个项目被选中,但焦点在其他地方。
I thought this could be because the focus is being set before the databound items are loaded into it... but the following code shows that there are indeed items because ctrlItemsCount
shows the number 8.
我认为这可能是因为焦点是在数据绑定项加载到它之前设置的......但是下面的代码显示确实存在项目,因为ctrlItemsCount显示数字8。
How can I set the initial focus in this situation, and what is the correct place to set initial focus usually?
如何在这种情况下设置初始焦点,通常设置初始焦点的正确位置是什么?
private void onLoad(object sender, RoutedEventArgs e) {
if (ctrlCountries.Items.Count > 0) {
ctrlItemsCount.Text = ctrlCountries.Items.Count;
ctrlCountries.SelectedIndex = 0;
FocusManager.SetFocusedElement(this, ctrlCountries);
}
}
EDIT: I have moved this code to the loaded event for the actual ListBox
itself. It almost works - the focus is now on the ListBox
, but I still need to press keyboard DOWN once before item #0 has the keyboard cursor. In other words, the focus, or cursor, is 1 notch above item #0 for some reason:
编辑:我已将此代码移动到实际ListBox本身的加载事件。它几乎可以工作 - 焦点现在在ListBox上,但我仍然需要在项目#0具有键盘光标之前按下键盘一次。换句话说,由于某种原因,焦点或光标在项目#0上方1个档位:
private void onCountriesLoaded(object sender, RoutedEventArgs e) {
ctrlCountries.SelectedIndex = 0;
FocusManager.SetFocusedElement(this, ctrlCountries);
Keyboard.Focus();
}
2 个解决方案
#1
10
If you want to focus the first element in the list box you have to set the focus to the first ListBoxItem container. For example:
如果要将第一个元素聚焦在列表框中,则必须将焦点设置为第一个ListBoxItem容器。例如:
if (myListBox.Items.Count > 0)
{
ListBoxItem item = (ListBoxItem)myListBox.ItemContainerGenerator.ContainerFromIndex(0);
FocusManager.SetFocusedElement(this /* focus scope region */, item);
}
You still have to make sure though, that the ListBox control has first received its Loaded event. There are a number of other events that are useful for handling list box item related updates. Have a look at the ItemContainerGenerator in MSDN.
但是,您仍然必须确保ListBox控件首次收到其Loaded事件。还有许多其他事件可用于处理与列表框项相关的更新。看看MSDN中的ItemContainerGenerator。
#2
2
The FocusManager.SetFocusedElement
method gives logical focus, but not keyboard focus. You can use the Keyboard.Focus
method to give keyboard focus to an element. Have a look at this page for more details about focus management in WPF.
FocusManager.SetFocusedElement方法提供逻辑焦点,但不提供键盘焦点。您可以使用Keyboard.Focus方法为元素提供键盘焦点。有关WPF焦点管理的更多详细信息,请查看此页面。
#1
10
If you want to focus the first element in the list box you have to set the focus to the first ListBoxItem container. For example:
如果要将第一个元素聚焦在列表框中,则必须将焦点设置为第一个ListBoxItem容器。例如:
if (myListBox.Items.Count > 0)
{
ListBoxItem item = (ListBoxItem)myListBox.ItemContainerGenerator.ContainerFromIndex(0);
FocusManager.SetFocusedElement(this /* focus scope region */, item);
}
You still have to make sure though, that the ListBox control has first received its Loaded event. There are a number of other events that are useful for handling list box item related updates. Have a look at the ItemContainerGenerator in MSDN.
但是,您仍然必须确保ListBox控件首次收到其Loaded事件。还有许多其他事件可用于处理与列表框项相关的更新。看看MSDN中的ItemContainerGenerator。
#2
2
The FocusManager.SetFocusedElement
method gives logical focus, but not keyboard focus. You can use the Keyboard.Focus
method to give keyboard focus to an element. Have a look at this page for more details about focus management in WPF.
FocusManager.SetFocusedElement方法提供逻辑焦点,但不提供键盘焦点。您可以使用Keyboard.Focus方法为元素提供键盘焦点。有关WPF焦点管理的更多详细信息,请查看此页面。