确保按钮在ScrollViewer中获得焦点

时间:2015-11-17 12:53:22

标签: wpf button windows-7 scrollviewer arrow-keys

哎呀!完全忘记了我的应用程序范围,这是按钮上的边距。我修改了代码和gif以反映这一点。

我有以下XAML标记:

<Window x:Name="SampleMainWindow1" x:Class="SampleApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="300">
<DockPanel>

    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Grid.Row="1">
            <StackPanel Orientation="Horizontal">
                <Button Margin="40,40,40,40" Width="Auto">Button 1</Button>
                <Button Margin="40,40,40,40" Width="Auto">Button 2</Button>
                <Button Margin="40,40,40,40" Width="Auto">Button 3</Button>
                <Button Margin="40,40,40,40" Width="Auto">Button 4</Button>
            </StackPanel>
        </ScrollViewer>

    </Grid>

</DockPanel>

然而,当一个按钮接收到Button 1等焦点时,我按下右键,我立即专注于Button 2,但是你可以看到,我必须再向右按几次在Button 3收到焦点之前,同样适用于Button 4

怎样才能使按键立即聚焦在下一个按钮上,该按钮会滚动到视图中?

Example

1 个答案:

答案 0 :(得分:3)

通过将ScrollViewer.CanContentScroll设置为true来滚动面板。