首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF MeasureOverride环路

WPF MeasureOverride环路
EN

Stack Overflow用户
提问于 2015-06-09 08:02:07
回答 1查看 864关注 0票数 1

这就是背景:

Creating a WPF divided diagram

我已经决定删除滚动器,并且只使用一个将随着基数的变化而增加的滚动器。

我有一个很奇怪的问题要解决。就是这样,我用这种方式设置了一个项目。

父母->儿童。

ScrollViewer ->画布。

画布->网格

网格->多个画布(分为不同的网格列)。

代码语言:javascript
复制
<ScrollViewer x:Name="MainScrollView" HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                <s:DesignerCanvas Focusable="true" x:Name="MyDesignerTop"
                        FocusVisualStyle="{x:Null}" IsHitTestVisible="True"
                        Grid.ColumnSpan="2147483647" Panel.ZIndex="2147483647"
                        ContextMenu="{StaticResource DesignerCanvasContextMenu}">

                    <s:DesignerCanvas.Background>
                        <SolidColorBrush Opacity="0"/>
                    </s:DesignerCanvas.Background>
                    <Grid x:Name="MainGrid" Background="#FF61B5B9">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                        Background="{StaticResource WindowBackgroundBrush}"
                        FocusVisualStyle="{x:Null}" Width="700" Height="700"
                         Margin="5"/>
                        <!--ContextMenu="{StaticResource DesignerCanvasContextMenu}"-->

                    </Grid>
                </s:DesignerCanvas>

</ScrollViewer>

我会解释,我在一个较低的画布上有一个项目,假设我想把它从一个帆布移到另一个画布上,我设法做到了,因为我有一个更高的画布,这让我可以。

问题是,当我想调整项目的大小时,让我们假设一个较低的画布上的一个项目正在向边缘移动,我可以重新绘制画布,但是这对上面的网格没有影响(有时奇怪),所以我的上画布没有任何效果,所以我的滚动查看器没有通知尺寸的增加。

如果我手动告诉网格测量它自己,因为其中一个元素childs超过了划分的画布大小,那么它也会告诉它的所有子元素测量自己,以便返回他们想要的值,这将导致无限循环。

下面是MeasureOverride函数:

代码语言:javascript
复制
protected override Size MeasureOverride(Size constraint)
    {
        Size size = new Size();

        foreach (UIElement element in this.InternalChildren)
        {
            double left = Canvas.GetLeft(element);
            double top = Canvas.GetTop(element);
            left = double.IsNaN(left) ? 0 : left;
            top = double.IsNaN(top) ? 0 : top;

            //measure desired size for each child
            element.Measure(constraint);

            Size desiredSize = element.DesiredSize;
            if (!double.IsNaN(desiredSize.Width) && !double.IsNaN(desiredSize.Height))
            {
                size.Width = Math.Max(size.Width, left + desiredSize.Width);
                size.Height = Math.Max(size.Height, top + desiredSize.Height);
            }
        }
        // add margin 
        size.Width += 10;
        size.Height += 10;

        //Let's assume here I will tell the grid to manually measure itself -> infinite loop

        return size;
    }

我怎么才能解决这个问题?如何才能通知链中的所有儿童在不引起无限循环的情况下测量自己?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-09 08:16:27

我奇怪地解决了这个问题。我不再使用宽度,只使用MinWidth,所有的计算都突然被修正了。

代码语言:javascript
复制
<ScrollViewer x:Name="MainScrollView" HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto"
                        HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

                <s:DesignerCanvas Focusable="true" x:Name="MyDesignerTop"
                        FocusVisualStyle="{x:Null}" IsHitTestVisible="True"
                        Grid.ColumnSpan="2147483647" Panel.ZIndex="2147483647"
                        ContextMenu="{StaticResource DesignerCanvasContextMenu}">

                    <s:DesignerCanvas.Background>
                        <SolidColorBrush Opacity="0"/>
                    </s:DesignerCanvas.Background>
                    <Grid x:Name="MainGrid" Background="#FF61B5B9">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                        Background="{StaticResource WindowBackgroundBrush}"
                        FocusVisualStyle="{x:Null}" MinWidth="600" MinHeight="600"
                         Margin="5"/>
                        <!--ContextMenu="{StaticResource DesignerCanvasContextMenu}"-->

                    </Grid>
                </s:DesignerCanvas>

</ScrollViewer>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30726257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档