0

我想我已经了解 MeasureOverrride 的工作原理,但我试图在一个非常简单的情况下使用它并且它不起作用......所以现在我不太确定......使用 MeasureOverride 后我是否必须使用 Arrageoverride还是系统会为我做?情况是这样的:我有一个从 Panel 继承的 LinearLayout 类,它有两个名为 wrapwidht 和 wrapheigh 的字段,如果它们为 true,则 LinearLayout 的宽度或高度必须与其子级要求相同。所以我的 Measureoveride 看起来像:

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 归档是正确的..所以也许我不明白它是如何工作的。如果有人可以帮助我会非常好:-)

4

3 回答 3

2

在与您类似的上一篇文章中查看我的答案:WPF 和 Silverlight 中的两遍布局系统

答案是不,您不必重写 ArrangeOverride,但是如果您不打算使用 ArrangeOverride,那么使用 MeasureOverride 有什么意义呢

于 2011-04-14T12:37:14.023 回答
0

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

于 2011-04-14T12:35:31.227 回答
0

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

于 2017-04-14T07:30:09.493 回答