【问题标题】:Disable keyboard arrow navigation in a ListView在 ListView 中禁用键盘箭头导航
【发布时间】:2012-07-09 20:35:38
【问题描述】:

我有一个 ListView,我正在尝试禁用各个项目之间的键盘导航。 我尝试将 KeyboardNavigation.DirectionalNavigation="None" 设置为 ListView、其 PanelTemplate 以及单个项目。一个都没用。

此外,键盘方向导航也搞砸了——面板是一个包裹面板,按箭头导航会混乱,有时它也会在两个孩子之间循环,没有机会用箭头跳到其他孩子。这就是为什么我想禁用它,尽管纠正它会更好。

以下是相关代码:

<ListView IsTabStop="False" ItemsSource="{Binding Symbols}" SelectedItem="{Binding Selected,Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" KeyboardNavigation.DirectionalNavigation="None">

<ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" Width="210" Margin="0,12" KeyboardNavigation.DirectionalNavigation="None" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

 <ListView.ItemTemplate>
            <DataTemplate>
                <Border Width="24" Height="24" Background="White"
          BorderBrush="Black" BorderThickness="1 1 1 1" Margin="0"
                        KeyboardNavigation.IsTabStop="False" KeyboardNavigation.DirectionalNavigation="None">
                    <TextBlock Text="{Binding Text}" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="textBlock" FontFamily="Verdana, Arial"/>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>

【问题讨论】:

  • 使用 KeyboardNavigation.DirectionalNavigation="None" 进入列表框项

标签: wpf xaml keyboard


【解决方案1】:

只需为 PreviewKeyDown 事件添加一个处理程序:

    private void listView1_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        switch(e.Key)
        {
            case Key.Left:
            case Key.Right:
            case Key.Up:
            case Key.Down:
                e.Handled = true;
                break;
            default:
                break;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多