WPF / Silverlight中的可滚动网格

时间:2010-04-27 13:06:40

标签: wpf silverlight grid scrollviewer

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

我怀疑它与没有用正确的面板包装内部网格以及适当的大小调整(和调整大小)行为有关,这会迫使内部网格尊重滚动条,而不是简单渲染太大(并被其他窗口剪裁)。

托管窗口的定义如下:

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

1 个答案:

答案 0 :(得分:3)

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