ScrollView 内部的 StackPanel 物理滚动不正确

发布于 2024-12-01 00:11:10 字数 1522 浏览 0 评论 0原文

考虑以下 XAML(用户控件):

<Grid x:Name="LayoutRoot" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <Grid HorizontalAlignment="Right" Width="335.312" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ed:BlockArrow Fill="#FFF4F4F5" HorizontalAlignment="Left" Margin="0" Orientation="Left" Stroke="Black" Width="15" RenderTransformOrigin="5.6,-0.412" Height="12" VerticalAlignment="Center" MouseEnter="LeftArrow_MouseEnter" MouseLeave="LeftArrow_MouseLeave" MouseLeftButtonUp="LeftArrow_MouseLeftButtonUp"/>
        <ed:BlockArrow Fill="#FFF4F4F5" Stroke="Black" RenderTransformOrigin="5.6,-0.412" Height="12" VerticalAlignment="Center" MouseEnter="RightArrow_MouseEnter" MouseLeave="RightArrow_MouseLeave" HorizontalAlignment="Right" Width="15" MouseLeftButtonUp="RightArrow_MouseLeftButtonUp"/>
        <ScrollViewer x:Name="panelScrollViewer" Margin="15,0" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden">
            <StackPanel x:Name="slidingStackPanel" Orientation="Horizontal" Height="125.882" Width="305.312" ScrollViewer.VerticalScrollBarVisibility="Disabled" CanHorizontallyScroll="True" ScrollViewer.CanContentScroll="False"/>
        </ScrollViewer>
    </Grid>
</Grid>

我希望以物理方式滚动此堆栈面板。我动态地将元素添加到堆栈面板,这似乎工作正常。在另一个按钮的单击函数中,执行以下代码:

panelScrollViewer.ScrollToHorizontalOffset(100);

滚动视图和堆栈面板的水平偏移仍然为零!另外,stackpanel 的 ScrollOwner 为 null。有什么想法吗?

Consider the following XAML (a UserControl):

<Grid x:Name="LayoutRoot" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <Grid HorizontalAlignment="Right" Width="335.312" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ed:BlockArrow Fill="#FFF4F4F5" HorizontalAlignment="Left" Margin="0" Orientation="Left" Stroke="Black" Width="15" RenderTransformOrigin="5.6,-0.412" Height="12" VerticalAlignment="Center" MouseEnter="LeftArrow_MouseEnter" MouseLeave="LeftArrow_MouseLeave" MouseLeftButtonUp="LeftArrow_MouseLeftButtonUp"/>
        <ed:BlockArrow Fill="#FFF4F4F5" Stroke="Black" RenderTransformOrigin="5.6,-0.412" Height="12" VerticalAlignment="Center" MouseEnter="RightArrow_MouseEnter" MouseLeave="RightArrow_MouseLeave" HorizontalAlignment="Right" Width="15" MouseLeftButtonUp="RightArrow_MouseLeftButtonUp"/>
        <ScrollViewer x:Name="panelScrollViewer" Margin="15,0" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden">
            <StackPanel x:Name="slidingStackPanel" Orientation="Horizontal" Height="125.882" Width="305.312" ScrollViewer.VerticalScrollBarVisibility="Disabled" CanHorizontallyScroll="True" ScrollViewer.CanContentScroll="False"/>
        </ScrollViewer>
    </Grid>
</Grid>

I am looking to physically scroll this stackpanel. I dynamically add elements to the stackpanel, and this appears to work correctly. In another button's click function, the following code is executed:

panelScrollViewer.ScrollToHorizontalOffset(100);

The scrollview and the stackpanel's HorizonalOffset is still zero! Also, the stackpanel's ScrollOwner is null. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请爱~陌生人 2024-12-08 00:11:10

由于您没有指定 ScrollViewer 的宽度,因此它采用 StackPanel 的大小。因为 ScrollViewer 的内容不大于其大小,所以它不会滚动,并且水平偏移始终为 0。尝试将 ScrollViewer 的宽度设置为小于其内容,您应该有一个有效的水平滚动。

还要删除禁用垂直滚动条的所有冗余,一次就足够了。

<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Right" Width="335.312">
    <ed:BlockArrow Fill="#FFF4F4F5" HorizontalAlignment="Left" Margin="0" 
         Orientation="Left" Stroke="Black" Width="15" RenderTransformOrigin="5.6,-0.412"
         Height="12" VerticalAlignment="Center" MouseEnter="LeftArrow_MouseEnter" 
         MouseLeave="LeftArrow_MouseLeave" MouseLeftButtonUp="LeftArrow_MouseLeftButtonUp"/>
    <ed:BlockArrow Fill="#FFF4F4F5" Stroke="Black" RenderTransformOrigin="5.6,-0.412" 
         Height="12" VerticalAlignment="Center" MouseEnter="RightArrow_MouseEnter" 
         MouseLeave="RightArrow_MouseLeave" HorizontalAlignment="Right" Width="15" 
         MouseLeftButtonUp="RightArrow_MouseLeftButtonUp"/>
    <ScrollViewer x:Name="panelScrollViewer" Margin="15,0" CanContentScroll="False" 
          VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden"
          Width="250">
        <StackPanel x:Name="slidingStackPanel" Orientation="Horizontal" Height="125.882" Width="305.312"/>
    </ScrollViewer>
</Grid>

Since you do not specify a width of the ScrollViewer, it takes the size of the StackPanel. Because the content of the ScrollViewer is not larger than its size then it will not scroll and will always have a horizontal offset of 0. Try setting the width of the ScrollViewer to something smaller than its contents and you should have a working horizontal scroll.

Also remove all the redundancy of disabling the vertical scroll bars, once is good enough.

<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Right" Width="335.312">
    <ed:BlockArrow Fill="#FFF4F4F5" HorizontalAlignment="Left" Margin="0" 
         Orientation="Left" Stroke="Black" Width="15" RenderTransformOrigin="5.6,-0.412"
         Height="12" VerticalAlignment="Center" MouseEnter="LeftArrow_MouseEnter" 
         MouseLeave="LeftArrow_MouseLeave" MouseLeftButtonUp="LeftArrow_MouseLeftButtonUp"/>
    <ed:BlockArrow Fill="#FFF4F4F5" Stroke="Black" RenderTransformOrigin="5.6,-0.412" 
         Height="12" VerticalAlignment="Center" MouseEnter="RightArrow_MouseEnter" 
         MouseLeave="RightArrow_MouseLeave" HorizontalAlignment="Right" Width="15" 
         MouseLeftButtonUp="RightArrow_MouseLeftButtonUp"/>
    <ScrollViewer x:Name="panelScrollViewer" Margin="15,0" CanContentScroll="False" 
          VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Hidden"
          Width="250">
        <StackPanel x:Name="slidingStackPanel" Orientation="Horizontal" Height="125.882" Width="305.312"/>
    </ScrollViewer>
</Grid>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文