在列表框项目设置重点突破的键盘导航 [英] Setting focus on a ListBox item breaks keyboard navigation
问题描述
选择列表框项目编程它是需要preSS下跌\\向上键后的两次以移动选择。有什么建议?
查看:
<列表框名称=lbActionsCanvas.Left =10Canvas.Top =10
WIDTH =260HEIGHT =180>
&所述; ListBoxItem的名称=打开IsSelected =真的内容=打开>&下; / ListBoxItem的>
< ListBoxItem的名称=输入内容=输入>< / ListBoxItem的>
&所述; ListBoxItem的名称=打印内容=打印>&下; / ListBoxItem的>
< /列表框>
code:
公开查看()
{
lbActions.Focus();
lbActions.SelectedIndex = 0; //没有帮助
((ListBoxItem中)lbActions.SelectedItem)。重点(); //没有任何帮助
}
不要将焦点设置到ListBox ...将焦点设置到选定的ListBoxItem的。这将解决需要两个键盘敲击的问题:
如果(lbActions.SelectedItem!= NULL)
((ListBoxItem中)lbActions.SelectedItem)。重点();
其他
lbActions.Focus();
如果您的列表框包含比 ListBoxItem的
什么东西一样,你可以使用 lbActions.ItemContainerGenerator.ContainerFromIndex(lbActions.SelectedIndex)
来获取自动生成的 ListBoxItem的
。
如果您希望这期间发生的窗口初始化的,你需要把code在加载
事件,而不是进入构造函数。示例(XAML):
<窗口...加载=Window_Loaded>
...
< /窗GT;
code(基于例如在你的问题):
私人无效Window_Loaded(对象发件人,RoutedEventArgs E)
{
lbActions.Focus();
lbActions.SelectedIndex = 0;
((ListBoxItem中)lbActions.SelectedItem)。重点();
}
After selecting ListBox item programmatically it is needed to press down\up key two times to move the selection. Any suggestions?
View:
<ListBox Name="lbActions" Canvas.Left="10" Canvas.Top="10"
Width="260" Height="180">
<ListBoxItem Name="Open" IsSelected="true" Content="Open"></ListBoxItem>
<ListBoxItem Name="Enter" Content="Enter"></ListBoxItem>
<ListBoxItem Name="Print" Content="Print"></ListBoxItem>
</ListBox>
Code:
public View()
{
lbActions.Focus();
lbActions.SelectedIndex = 0; //not helps
((ListBoxItem) lbActions.SelectedItem).Focus(); //not helps either
}
Don't set the focus to the ListBox... set the focus to the selected ListBoxItem. This will solve the "two keyboard strokes required" problem:
if (lbActions.SelectedItem != null)
((ListBoxItem)lbActions.SelectedItem).Focus();
else
lbActions.Focus();
If your ListBox contains something else than ListBoxItem
s, you can use lbActions.ItemContainerGenerator.ContainerFromIndex(lbActions.SelectedIndex)
to get the automatically generated ListBoxItem
.
If you want this to happen during window initialization, you need to put the code in the Loaded
event rather than into the constructor. Example (XAML):
<Window ... Loaded="Window_Loaded">
...
</Window>
Code (based on the example in your question):
private void Window_Loaded(object sender, RoutedEventArgs e)
{
lbActions.Focus();
lbActions.SelectedIndex = 0;
((ListBoxItem)lbActions.SelectedItem).Focus();
}
这篇关于在列表框项目设置重点突破的键盘导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!