禁用ListView中的键盘箭头导航

时间:2012-07-10 11:14:48

标签: wpf xaml keyboard

我有一个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>

1 个答案:

答案 0 :(得分: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;
        }
    }