在确定大小时使 WPF/SL 网格忽略子元素 [英] Make WPF/SL grid ignore a child element when determining size

查看:8
本文介绍了在确定大小时使 WPF/SL 网格忽略子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Grid,它有几个孩子,其中一个是 ScrollViewer.我希望网格根据其所有子项 自行调整大小,ScrollViewer 除外,我只想占用通过为其余子项调整网格大小而创建的空间.(例如,Grid 为 2x2,ScrollViewer 位于第 0 行、第 0 列,因此其他三个 Grid 条目足以确定 Grid 的尺寸.)

I have a Grid which has several children, one of which is a ScrollViewer. I want the Grid to size itself based on all of its children except the ScrollViewer, which I want to just take up the space that is created by sizing the Grid for the rest of its children. (For example, the Grid is 2x2 and the ScrollViewer is in Row 0, Column 0, and so the other three Grid entries are enough to determine the dimensions of the Grid.)

有什么好的方法可以做到这一点吗?我已经研究过创建一个自定义面板来替换网格或包含 ScrollViewer,但是在 MeasureOverride/ArrangeOverride 调用期间,我从来没有接到过可以告诉我网格行的最终宽度/高度的电话/column 我关心的(例如,第 0 行,第 0 列).

Is there a nice way to do this? I've looked into creating a Custom Panel either to replace the Grid or to contain the ScrollViewer, but at no point during the MeasureOverride/ArrangeOverride calls do I ever get a call which can tell me about the final width/height of the grid row/column I care about (eg, Row 0, Column 0).

我的一个想法是从 Grid 派生并调用基本的 MeasureOverride/ArrangeOverride,但在调用之前从 Grid 的 Children 中删除 ScrollViewer(然后将其放回去),但在布局计算期间弄乱了可视化树似乎是个坏主意.

One thought I had was to derive from Grid and call the base MeasureOverride/ArrangeOverride but remove the ScrollViewer from the Children of the Grid right before the call (and put it back afterwards), but messing with the visual tree during layout computation seems like a bad idea.

这是一个例子:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid VerticalAlignment="Top" HorizontalAlignment="Left">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ScrollViewer Grid.Row="0" Grid.Column="0" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <Button Height="300" Width="300"/>
        </ScrollViewer>
        <Button Grid.Row="1" Grid.Column="0" Height="100" Width="100" Content="1,0"/>
        <Button Grid.Row="0" Grid.Column="1" Height="100" Width="100" Content="0,1"/>
        <Button Grid.Row="1" Grid.Column="1" Height="100" Width="100" Content="1,1"/>
    </Grid>
</Grid>

我希望 Grid 的大小不会随着 ScrollViewer 中 Button 的大小发生变化而改变 - 例如,我希望 Grid 为 ScrollViewer 提供由其余 Grid 内容确定的 100x100 正方形.如果我将 3 个 100x100 按钮中的每一个都更改为 200x200,我希望 ScrollViewer 获得 200x200 等.

I would like the size of the Grid to not change as the size of the Button in the ScrollViewer changes - e.g., I want the Grid to give the ScrollViewer the 100x100 square that is determined by the rest of the Grid's contents. And if I changed each of the 3 100x100 buttons to be 200x200, I would want the ScrollViewer to get 200x200, etc.

Pavlo 的例子是我目前最接近的,网格行/列的大小适当,但 ScrollViewer 不适应调用排列时给它的大小.见下文:

Pavlo's example gets me the closest so far, with the Grid rows/columns appropriately sized, but ScrollViewer does not adapt to the Size given to it when Arrange is called. See below:

推荐答案

如果我正确理解您想要什么,那么您可以执行以下技巧.创建一个装饰器,它会在 Measure 阶段要求 0 空间,并在排列阶段用所有给定的空间来安排孩子:

If I understood correctly what you want, then you can do the following trick. Create a decorator that will ask for 0 space during the Measure stage and will arrange the child with all the given space at the Arrange stage:

public class NoSizeDecorator : Decorator
{
    protected override Size MeasureOverride(Size constraint) {
        // Ask for no space
        Child.Measure(new Size(0,0));
        return new Size(0, 0);
    }        
}

您的 XAML 将如下所示:

And your XAML will look like this:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid VerticalAlignment="Top" HorizontalAlignment="Left">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <my:NoSizeDecorator Grid.Row="0" Grid.Column="0">
            <ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <Button Height="300" Width="300" Content="0,0"/>
            </ScrollViewer>
        </my:NoSizeDecorator>

        <Button Grid.Row="1" Grid.Column="0" Height="100" Width="100" Content="1,0"/>
        <Button Grid.Row="0" Grid.Column="1" Height="100" Width="100" Content="0,1"/>
        <Button Grid.Row="1" Grid.Column="1" Height="100" Width="100" Content="1,1"/>
    </Grid>
</Grid>

这篇关于在确定大小时使 WPF/SL 网格忽略子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆