5

我在使水平堆棧面板從scrollviewer內滾動時遇到問題。我想要做的是比我的例子更復雜,但是在刪除變量之後,我認爲如果我能解決最後一個問題,我可以把所有東西都弄清楚。XAML水平堆棧面板滾動

基本上,我不能讓scrollviewer水平滾動時,它包含一個水平的stackpanel。

下面是示例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> 

奇怪的是,如果我只是換了方向,從水平到垂直,它滾動就好了。我已經閱讀過多篇文章,表示stackpanels有問題,可能會使它們不適合scrollviewers,所以我嘗試了一個網格,但獲得相同的結果。

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

看起來很簡單,我覺得我誤解了一些基本的東西。如果有人能幫忙,我會非常感激。

回答

8

你只需打開水平滾動。 It's hidden by default(但垂直的不是,因此混亂)。

<ScrollViewer HorizontalScrollBarVisibility="Auto"> 
+2

謝謝!我瀏覽了無數關於Horizo​​ntalScrollBarVisibility的帖子,甚至查看了您發佈的MSDN鏈接,但我從來沒有意識到滾動工作需要滾動條可見性。 非常感謝,我花了好幾天時間。 – Riot9

+1

令人發瘋的是,即使啓用了「Horizo​​ntalScrollMode」,也必須啓用「Horizo​​ntalScrollBarVisibility」以使基於觸摸的滾動工作。 –