在网格大小中使用*的问题

时间:2010-01-14 21:04:28

标签: wpf layout grid scrollbar autosize

当我将列的宽度或行的高度设置为星形,并且我填充的内容大于可用的矩形时,该行继续在“地下”增长:

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="100" Width="100">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel>
            <Button Content="Button" />
            <Button Content="Button" />
            <Button Content="Button" />
        </StackPanel>
        <StackPanel Grid.Row="1" 
                ScrollViewer.CanContentScroll="True" 
                ScrollViewer.VerticalScrollBarVisibility="Auto" 
                ScrollViewer.IsDeferredScrollingEnabled="True">
            <Button Content="Button" />
            <Button Content="Button" />
            <Button Content="Button" />
        </StackPanel>
    </Grid>
</Window>

我希望StackPanel不应该在地下生长,而应该显示滚动条。

注意,我需要动态的所有尺寸,所以一切都会根据用户调整父母的大小而改变 我对列有同样的问题 PS。 RowDefinition.Height默认为*。

更新

我猜问题是网格和我发布的上一个片段没有提供整个故事,请查看:

<Window x:Class="Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"            
Title="Window1" Height="200">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition/>
            <RowDefinition Height="20"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="1">
            <StackPanel>
                <TextBlock Text="Hello"/>
                <TextBox Text="World!"/>
            </StackPanel>
            <ScrollViewer>
                <StackPanel>
                    <Button Content="Button"/>
                    <Button Content="Button"/>
                    <Button Content="Button"/>
                    <Button Content="Button"/>
                    <Button Content="Button"/>
                </StackPanel>
            </ScrollViewer>
        </StackPanel>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:2)

我不确定StackPanel是否内置了ScrollViewer,因此设置ScrollViewer.*附加的proeprties对它没有任何影响。

您是否尝试过StackPanel明确包装ScrollViewer?例如:

<ScrollViewer Grid.Row="1"
       ScrollViewer.CanContentScroll="True" 
       ScrollViewer.VerticalScrollBarVisibility="Auto" 
       ScrollViewer.IsDeferredScrollingEnabled="True">
    <StackPanel>
        <Button Content="Button" />
        <Button Content="Button" />
        <Button Content="Button" />
    </StackPanel>
</ScrollViewer>