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

发布于 2024-08-18 09:57:03 字数 2364 浏览 7 评论 0原文

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

<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 不应该在地下增长,而是应该显示滚动条。

请注意,我需要所有动态大小,因此一切都会根据用户调整父级大小而变化。
我对列也有同样的问题。
附言。 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>

When I set a column's width or a row's height to star, and I fill in content that is bigger than the available rectangle, the row keeps on growing 'underground':

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

I want that StackPanel should not grow underground, instead it should show scrollbars.

Note, I need all the size dynamic so everything changes according to the user resizing the parents.
I have the same issue with columns.
PS. RowDefinition.Height is always * by default.

UPDATE

I guess the problem is the grid and the previous snippet I posted didn't provide the whole story, please review:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-08-25 09:57:03

我不确定 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>

I'm not sure that StackPanel has a built in ScrollViewer so setting the ScrollViewer.* attached proeprties really has no effect on it.

Have you tried wrapping the StackPanel with a ScrollViewer explicitly? For example:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文