在列表框项目设置重点突破的键盘导航 [英] Setting focus on a ListBox item breaks keyboard navigation

查看:142
本文介绍了在列表框项目设置重点突破的键盘导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择列表框项目编程它是需要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 ListBoxItems, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆