2009-12-03 51 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