2010-04-27 67 views
1

我想建立一个WPF窗口,它使用外部网格屏幕分成4部分。在右下象限中,我想嵌入另一个大于网格单元格的网格。我一直在寻找添加ScrollViewer(或使用Grid.ScrollViewer属性)的方法,但无论我尝试什么,内部网格不会调整大小或适当地显示滚动条WPF/Silverlight中的可滚动网格

我怀疑它与有关,没有用适当的尺寸(和调整大小)行为包装内部网格这将迫使内部网格兑现滚动条,而不是简单地渲染太大(并被另一个窗口夹住)。

托管窗口的定义是这样的:

<Window x:Class="GridScrollTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:GridScrollTest" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid x:Name="OuterGrid"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="100" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 
     <local:SSControl x:Name="Sheet" 
         Grid.Row="1" Grid.Column="1" 
         HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Yellow" /> 
     <Canvas Grid.Row="0" Grid.Column="0" Background="LightGreen" /> 
     <Canvas Grid.Row="1" Grid.Column="0" Background="LightBlue" /> 
     <Canvas Grid.Row="0" Grid.Column="1" Background="LightCoral" /> 
    </Grid> 
</Window> 

和引用sscontrol的:

<UserControl x:Class="GridScrollTest.SSControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    Height="270" Width="600"> 
    <ScrollViewer 
     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
     CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> 
     <Grid x:Name="CellGrid" ShowGridLines="False" 
       > 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
       <ColumnDefinition Width="100" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
      </Grid.RowDefinitions> 
     </Grid> 
    </ScrollViewer> 
</UserControl> 
+0

你的结果到底是什么?你说*“内部网格不调整或适当地显示滚动条”*,但从你的解释,我认为你不希望它调整大小,是吗?我对您的描述的理解是,您希望网格以实际大小显示,跨越窗口的边界,并且将滚动条添加到其中,以便用户可以滚动可查看的部分。是对的吗?! – gehho 2010-04-27 15:33:13

回答

3

我不知道是肯定的,但试图在Blend你的代码后,我认为你的问题可能是因为您已将ColumnDefinition.WidthRowDefinition.Height设置为Auto。尝试将它们设置为*,并删除用户控件的Height=270Width=600。这样,外部网格填充窗口中的所有可用空间,右下角的单元格具有滚动条。

+0

将宽度和高度定义从Auto(这显然意味着无论您需要什么)更改为'*'(这似乎表示剩余空间)可以解决问题。谢谢! – 2010-04-27 17:22:05

+0

是的,这正是他们的意思。 **自动**表示单元格的内容说明需要多少空间,并且通常会获得该空间。 **星号**表示电池应占用剩余空间。您还可以让一列具有'宽度=“1 *”',另一列设置为'宽度=“2 *”'这意味着第一列将剩余空间的三分之一,第二列另外两列三分之二。当您需要在可调整大小的窗口中提供灵活的布局时,此功能非常强大。 – gehho 2010-04-28 06:51:54