首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Silverlight中使用MeasureOverride?

如何在Silverlight中使用MeasureOverride?
EN

Stack Overflow用户
提问于 2011-04-14 20:26:46
回答 3查看 1.2K关注 0票数 0

我想我已经理解了MeasureOverrride是如何工作的,但我试图在一个非常简单的情况下使用它,但它不起作用……所以现在我不太确定..。使用测量覆盖后,我是否也必须使用Arrageoverride,否则系统会为我这样做?情况是这样的:我有一个从面板继承的LinearLayout类,它有两个字段叫做wrapwidht和wrapheigh,如果它们是真的,那么LinearLayout的宽度或高度必须是它的子元素所要求的。因此,我的Measureoveride如下所示:

代码语言:javascript
复制
protected override Size MeasureOverride(Size availableSize) {

    Size panelDesiredSize = new Size();


    if ((this.widthWrap) || (this.heightWrap))
    {

        foreach (UIElement elemento in this.Children)
        {

            System.Diagnostics.Debug.WriteLine(((FrameworkElement)elemento).DesiredSize.ToString());

            if (this.Orientation.Equals(System.Windows.Controls.Orientation.Vertical))
            {
                if (this.widthWrap)
                {
                    //the widest element will determine containers width
                    if (panelDesiredSize.Width < ((FrameworkElement)elemento).Width)
                        panelDesiredSize.Width = ((FrameworkElement)elemento).Width;
                }
                //the height of the Layout is determine by the sum of all the elment that it cointains
                if (this.heightWrap)
                    panelDesiredSize.Height += ((FrameworkElement)elemento).Height;
            }
            else
            {
                if (this.heightWrap)
                {
                    //The highest will determine the height of the Layout
                    if (panelDesiredSize.Height < ((FrameworkElement)elemento).Height)
                        panelDesiredSize.Height = ((FrameworkElement)elemento).Height;
                }

                //The width of the container is the sum of all the elements widths
                if (this.widthWrap)
                    panelDesiredSize.Width += ((FrameworkElement)elemento).Width;
            }

        }
    }

    System.Diagnostics.Debug.WriteLine("desiredsizzeeeeeee" + panelDesiredSize);

    return panelDesiredSize;

}

我带到LinerLayout的子按钮是3个按钮,但没有绘制任何内容。即使panelDesiredSize字段是正确的..所以也许我不太明白它是如何工作的。如果有人能帮我,我会很高兴的:-)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-14 20:37:14

在之前类似于你的帖子上查看我的答案:Two Pass Layout system in WPF and Silverlight

答案是否定的,你没有来覆盖ArrangeOverride,但是如果你不打算使用MeasureOverride,那么使用ArrangeOverride有什么意义呢?

票数 2
EN

Stack Overflow用户

发布于 2011-04-14 20:35:31

您应该对子对象调用度量方法。请参阅此处的示例:http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.measureoverride.aspx

票数 0
EN

Stack Overflow用户

发布于 2017-04-14 15:30:09

您必须在"elemento“上调用Measure。正是在测量期间,Silverlight创建了在elemento的模板中声明的UI元素,因为需要它们来实际测量并得出所需的大小。然后,您应该使用elemento.DesiredSize.Width和Height为您的panelDesiredSize提供所需的大小。

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

https://stackoverflow.com/questions/5663209

复制
相关文章

相似问题

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