【问题标题】:StackPanel inside of a ScrollView doesnt physically scroll correctlyScrollView 内的 StackPanel 无法正确滚动
【发布时间】:2011-08-24 16:49:23
【问题描述】:

考虑以下 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);

滚动视图和堆栈面板的 Horizo​​nalOffset 仍然为零!此外,堆栈面板的 ScrollOwner 为空。有什么想法吗?

【问题讨论】:

    标签: c# wpf xaml scrollview stackpanel


    【解决方案1】:

    由于您没有指定 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>
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多