首页 > 解决方案 > 如何在 WPF 中使用键盘导航离开 ScrollViewer 的焦点

问题描述

我遇到了键盘导航和ScrollViewer. 这是我的设置:

<DockPanel>
    <Button DockPanel.Dock="Top" Content="Add" />
    <Button DockPanel.Dock="Top" Content="Clear" />
    <ItemsControl DockPanel.Dock="Top" Focusable="False" 
                  ScrollViewer.CanContentScroll="True"
                  ItemsSource="{Binding ItemsList}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Item.Name}" 
                          IsChecked="{Binding Selected}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.Template>
            <ControlTemplate>
                <ScrollViewer Focusable="False">
                    <ItemsPresenter />
                </ScrollViewer>
            </ControlTemplate>
        </ItemsControl.Template>
    </ItemsControl>
</DockPanel>

使用向上和向下箭头键移动焦点可以正常工作,直到焦点进入我的ItemsControls. 然后我只能在里面移动焦点。例如,如果我到达 ItemsControls 的顶部项目,我希望焦点跳回我的“清除”按钮,但它没有,它卡在 ItemsControl 内。我尝试了各种KeyboardNavigation.DirectionalNavigation选择,但没有任何效果。

删除 ScrollViewer 修复了导航,但这显然不可行,因为我需要显示更多适合屏幕的项目。

有任何想法吗?

标签: wpfxaml

解决方案


推荐阅读