【问题标题】:XAML Horizontal Stackpanel ScrollingXAML 水平堆栈面板滚动
【发布时间】:2026-01-21 10:05:01
【问题描述】:

我在让水平堆栈面板从滚动查看器中滚动时遇到问题。我想做的比我的例子更复杂,但是在删除变量之后,我想如果我能解决最后一个问题,我可以弄清楚一切。

基本上,当滚动查看器包含水平堆栈面板时,我无法让它水平滚动。

这里是示例 XAML:

   <ScrollViewer>
        <StackPanel Orientation="Horizontal">
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
            <Image Source="test.png" Width="400" Height="400"/>
        </StackPanel>
    </ScrollViewer>

奇怪的是,如果我只是将方向从水平切换到垂直,它就可以很好地滚动。我在多篇文章中读到堆栈面板存在问题,可能使它们不适合滚动查看器,因此我也尝试使用网格,但得到相同的结果。

  <ScrollViewer>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="400"/>
            </Grid.ColumnDefinitions>                
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="0"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="1"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="2"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="3"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="4"/>
            <Image Source="test.png" Width="400" Height="400"  Grid.Column="5"/>
        </Grid>
    </ScrollViewer>

这似乎很简单,我觉得我误解了一些基本的东西。如果有人能帮忙,我将非常感激。

【问题讨论】:

    标签: xaml windows-8 windows-runtime horizontal-scrolling stackpanel


    【解决方案1】:

    您只需要打开水平滚动即可。 It's hidden by default(但垂直的不是,因此混乱)。

    <ScrollViewer HorizontalScrollBarVisibility="Auto">
    

    【讨论】:

    • 谢谢!我浏览了无数关于 Horizo​​ntalScrollBarVisibility 的帖子,甚至查看了您发布的 MSDN 链接,但我从未意识到滚动工作需要滚动条可见性。非常感谢,我花了好几天的时间。
    • 疯狂的是,必须启用HorizontalScrollBarVisibility 才能使基于触摸的滚动工作,即使启用了HorizontalScrollMode