2011-08-24 75 views
0

考虑下面的XAML(一个用户控件):StackPanel的滚动型的内犯规物理滚动正确

<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> 

我期待物理滚动这个StackPanel中。我动态地将元素添加到堆栈面板,并且这似乎正常工作。在另一个按钮的点击功能中,执行以下代码:

panelScrollViewer.ScrollToHorizontalOffset(100); 

scrollview和stackpanel的Horizo​​nalOffset仍然为零!此外,堆栈面板的ScrollOwner为空。有任何想法吗?

回答

3

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

+0

谢谢,这个工作 – Jason