2009-12-03 73 views
6

我有一個電網一個的ScrollViewer邊境一個的StackPanel窗口內內。如何讓我的ScrollViewer滾動查看區域?

ScrollViewer將滾動條放在右側,但它是不可滾動

如何讓ScrollViewer使其內容可滾動?

alt text http://www.deviantsart.com/upload/1bl34e1.png

<Window x:Class="TestScroll234343.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="150" Width="300"> 
    <StackPanel> 
    <!--<StackPanel Height="150"> doesn't work either--> 
     <Border> 
      <ScrollViewer>    
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
         <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 

        <TextBlock Grid.Row="0" Grid.Column="0" Text="Row0"/> 
        <TextBlock Grid.Row="1" Grid.Column="0" Text="Row1"/> 
        <TextBlock Grid.Row="2" Grid.Column="0" Text="Row2"/> 
        <TextBlock Grid.Row="3" Grid.Column="0" Text="Row3"/> 
        <TextBlock Grid.Row="4" Grid.Column="0" Text="Row4"/> 
        <TextBlock Grid.Row="5" Grid.Column="0" Text="Row5"/> 
        <TextBlock Grid.Row="6" Grid.Column="0" Text="Row6"/> 
        <TextBlock Grid.Row="7" Grid.Column="0" Text="Row7"/> 
        <TextBlock Grid.Row="8" Grid.Column="0" Text="Row8"/> 
        <TextBlock Grid.Row="9" Grid.Column="0" Text="Row9"/> 
       </Grid> 
      </ScrollViewer> 
     </Border> 

    </StackPanel> 
</Window> 

回答

7

應設置ScrollViewer的高度。

如果你不這樣做,BorderScrollViewer做它想要的高度和ScrollViewer詢問Border它應該是什麼樣的高度。

其他選項:

  • 更改StackPanelDockPanel(它不會增長超過Window
  • 設置StackPanel的高度,並綁定到它在ScrollViewer

代碼:

<StackPanel Height="140"> 
    <Border> 
     <ScrollViewer Height="{Binding RelativeSource={RelativeSource FindAncestor, 
        AncestorType={x:Type StackPanel}}, Path=Height}"> 
+0

它工作,如果我設置ScrollViewer或邊框的高度,但不是StackPanel或窗口。無論如何要讓ScrollViewer成爲它所處的控制高度嗎? – 2009-12-03 16:08:26

+0

是的,請參閱編輯答案 – 2009-12-03 16:19:46

+0

+1,但我也注意到一個問題。在我的情況下,我將ScrollView與ListBox環繞,現在鼠標滾動只能沿滾動條滾動而不在列表框內。猜ListBox不涉及ScrollView。如何讓它在內容上滾動? (在我最初的設計中,我沒有指定ScrollViewer) – HoKy22 2015-07-30 16:57:30