2011-08-24 76 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