2016-12-14 52 views
0

我嘗試創建一個滾動的水平StackPanel的,但我沒有成功很好...爲StackPanel中水平滾動不起作用

目前,我有我有auto寬度的StackPanel(而問題是,也許在這裏)其中包含一些項目,如grids

現在,如果我的所有網格在stackpanel中都不可見(寬度太短),我無法滾動。 我已經嘗試將堆棧面板放入ScrollViewer中,但它不會在' 中工作。

我該如何dix呢?由於

編輯這裏是我的代碼:

<StackPanel Height="85" Margin="0,0,200,15" VerticalAlignment="Bottom"> 
     <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True"> 
      <StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition"> 
       <StackPanel.Background> 
        <SolidColorBrush Color="{DynamicResource ButtonBackground}"/> 
       </StackPanel.Background> 
       <Grid Width="100" Background="Red"/> 
       <Grid Width="100" Background="#FFFF0051"/> 
       <Grid Width="100" Background="#FFB900FF"/> 
       <Grid Width="100" Background="#FF002EFF"/> 
       <Grid Width="100" Background="#FF00FFDC"/> 
       <Grid Width="100" Background="#FF51FF00"/> 
       <Grid Width="100" Background="Red"/> 
      </StackPanel> 
     </ScrollViewer> 
    </StackPanel> 
+0

什麼是堆棧面板的祖先?你不能有自動寬度 – gmetax

+0

檢查滾動查看該線程它會幫助你http://stackoverflow.com/questions/19355818/how-to-make-scrollviewer-work-with-height-set-to-auto-in- wpf – gmetax

+2

它不能工作,除非你真的把整個東西放在ScrollViewer中。 – Clemens

回答

2

目前,我有我的自動寬度的StackPanel(問題可能在這裏),其中包含一些像網格這樣的項目。

This is your problem。如果一個StackPanel的Orientation屬性設置爲Horizo​​ntal,並且它的垂直空間設置爲Vertical,那麼它會測量其子節點的無限水平空間。所以你必須爲StackPanel本身或者ScrollViewer指定一個明確的寬度才能工作。

或者你可以把ScrollViewer中測量它的孩子,就像一個面板中,例如一個網格(但不是一個StackPanel)。這適用於例如:

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApplication1" 
    mc:Ignorable="d" 
    Title="Window" Height="300" Width="300"> 
<Grid> 
    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True"> 
     <StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition"> 
      <StackPanel.Background> 
       <SolidColorBrush Color="Yellow"/> 
      </StackPanel.Background> 
      <Grid Width="100" Background="Red"/> 
      <Grid Width="100" Background="#FFFF0051"/> 
      <Grid Width="100" Background="#FFB900FF"/> 
      <Grid Width="100" Background="#FF002EFF"/> 
      <Grid Width="100" Background="#FF00FFDC"/> 
      <Grid Width="100" Background="#FF51FF00"/> 
      <Grid Width="100" Background="Red"/> 
     </StackPanel> 
    </ScrollViewer> 
</Grid> 
</Window> 

但這並不因爲StackPanel中被認爲具有無限寬度:

<StackPanel> 
    <ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left" Height="85" CanContentScroll="True"> 
     <StackPanel x:Name="Film" Height="85" Width="Auto" Margin="0,0,0,0" Orientation="Horizontal" ScrollViewer.HorizontalScrollBarVisibility="Visible" CanHorizontallyScroll="True" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" d:LayoutOverrides="TopPosition, BottomPosition"> 
      <StackPanel.Background> 
       <SolidColorBrush Color="Yellow"/> 
      </StackPanel.Background> 
      <Grid Width="100" Background="Red"/> 
      <Grid Width="100" Background="#FFFF0051"/> 
      <Grid Width="100" Background="#FFB900FF"/> 
      <Grid Width="100" Background="#FF002EFF"/> 
      <Grid Width="100" Background="#FF00FFDC"/> 
      <Grid Width="100" Background="#FF51FF00"/> 
      <Grid Width="100" Background="Red"/> 
     </StackPanel> 
    </ScrollViewer> 
</StackPanel> 

內StackPanels把ScrollViewers是一個壞主意。

0

你應該把你的StackPanel成的ScrollViewer這樣的:

<ScrollViewer Height="85" VerticalAlignment="Bottom" Margin="0,0,200,15" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto"> 
    <StackPanel x:Name="Film" Orientation="Horizontal" > 
     <StackPanel.Background> 
      <SolidColorBrush Color="Black"/> 
     </StackPanel.Background> 
     <Grid Width="100" Background="Red"/> 
     <Grid Width="100" Background="#FFFF0051"/> 
     <Grid Width="100" Background="#FFB900FF"/> 
     <Grid Width="100" Background="#FF002EFF"/> 
     <Grid Width="100" Background="#FF00FFDC"/> 
     <Grid Width="100" Background="#FF51FF00"/> 
     <Grid Width="100" Background="Red"/> 
    </StackPanel> 
</ScrollViewer>