Wpf 在网格大小中使用*时出现问题

Wpf 在网格大小中使用*时出现问题,wpf,layout,grid,scrollbar,autosize,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"&g

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

<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。默认情况下,高度始终为*

更新 我想问题在于网格,我之前发布的代码片段没有提供全部内容,请查看:

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

我不确定
StackPanel
是否有内置的
ScrollViewer
,因此设置
ScrollViewer.
附加的项目实际上对它没有影响

您是否尝试过使用
ScrollViewer
显式包装
StackPanel
?例如:

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


非常抱歉,问题确实出在ScrollViewer上,但我想我还没有找到解决方案。请将您的
ScrollViewer
放在网格行中,不要将其嵌套在
堆栈面板
中。问题出在网格上。因为一个列表框肯定有一个内置的ScrollViewer,有时我想让列表框嵌套在StackPanel中。问题是StackPanel一直在地下生长,或者更好地说,RowHeight*下的任何东西都会在地下生长。问题是当ScrollViewer放置在StackPanel内时,其高度不受限制,这就是为什么您永远无法在其上获得垂直滚动条的原因。您需要将ScrollViewer直接放在网格行中,以便限制其高度,这样当其内容增长超过分配的高度时,您将获得滚动条。