本文章是由人工翻譯。 將指標移到文章內的文字上方即可查看原文。
譯文
原文
本主題尚未接受評分 - 為這個主題評分

FrameworkElement.MeasureOverride 方法

在衍生類別中覆寫時,為子項目測量所需配置的大小,並且判斷 FrameworkElement 所衍生類別的大小。

命名空間:  System.Windows
組件:  PresentationFramework (在 PresentationFramework.dll 中)
protected virtual Size MeasureOverride(
	Size availableSize
)

參數

availableSize
型別:System.Windows.Size
這個項目可以提供給子項目使用的大小。 您可以將 Infinity 指定為值,這個值表示項目大小可以調整至內容使用的任何大小。

傳回值

型別:System.Windows.Size
根據其對子項目大小的計算,此項目的大小決定其在配置期間的需求。

在其參與 Windows Presentation Foundation (WPF) 配置系統時覆寫 MeasureOverride,以便為您的項目實作自訂的配置縮放行為。 您的實作應該執行下列各項:

  1. 反覆查看項目的特定子系集合,這個集合是配置的一部分,會在每個子項目上呼叫 Measure

  2. 立即在子項目上取得 DesiredSize (如此一來會在呼叫 Measure 之後設定為屬性)。

  3. 根據子項目的測量,計算父系最終所需的大小。

MeasureOverride 的傳回值應該是項目擁有的所需大小,接著該值會變成目前項目之父項目的測量輸入。 這個相同的過程會繼續透過配置系統進行,直到到達頁面的根項目 (Root Element) 為止。

在這個過程中,子項目可能會傳回比初始 availableSize 更大的 DesiredSize 大小,表示子項目需要更多空間。 這個部分可能會在您所擁有的實作中處理,可藉由引入可捲動的區域、調整父控制項、建立一些堆疊順序的方法,或者不限數目的方案來進行測量或排列內容。

重要事項重要事項

在這個過程中,這些項目應該在每個子項目上呼叫 Measure,否則子項目將無法使用適當大小或進行正確的排列。

繼承者注意事項

下列未編譯程式碼示範這個實作模式。 VisualChildren 代表一個子系 (您應定義自己的項目) 的可列舉集合屬性。 屬性可以是任何名稱。 此例中,VisualChildren 是一個替代符號名稱,VisualChildren 不是由 WPF 或部分命名模式所提供的 API。


protected override Size MeasureOverride(Size availableSize)
{
    Size desiredSize = new Size();
    foreach (UIElement child in VisualChildren)
    {
        child.Measure(availableSize);
        // do something with child.DesiredSize, either sum them directly or apply whatever logic your element has for reinterpreting the child sizes
        // if greater than availableSize, must decide what to do and which size to return
    }
    // desiredSize = ... computed sum of children's DesiredSize ...;
    // IMPORTANT: do not allow PositiveInfinity to be returned, that will raise an exception in the caller!
    // PositiveInfinity might be an availableSize input; this means that the parent does not care about sizing
    return desiredSize;
}


This example shows how to override the default layout behavior of the Panel element and create custom layout elements that are derived from Panel.

The example defines a simple custom Panel element called PlotPanel, which positions child elements according to two hard-coded x- and y-coordinates. In this example, x and y are both set to 50; therefore, all child elements are positioned at that location on the x and y axes.

To implement custom Panel behaviors, the example uses the MeasureOverride and ArrangeOverride methods. Each method returns the Size data that is necessary to position and render child elements.


public class PlotPanel : Panel
{
    // Default public constructor
    public PlotPanel()
        : base()
    {
    }

    // Override the default Measure method of Panel
    protected override Size MeasureOverride(Size availableSize)
    {
        Size panelDesiredSize = new Size();

        // In our example, we just have one child. 
        // Report that our panel requires just the size of its only child.
        foreach (UIElement child in InternalChildren)
        {
            child.Measure(availableSize);
            panelDesiredSize = child.DesiredSize;
        }

        return panelDesiredSize ;
    }
    protected override Size ArrangeOverride(Size finalSize)
    {
        foreach (UIElement child in InternalChildren)
        {
            double x = 50;
            double y = 50;

            child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
        }
        return finalSize; // Returns the final Arranged size
    }
}


.NET Framework

支援版本:4、3.5、3.0

.NET Framework Client Profile

支援版本:4、3.5 SP1

Windows 7, Windows Vista SP1 (含) 以後版本, Windows XP SP3, Windows Server 2008 (不支援伺服器核心), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心), Windows Server 2003 SP2

.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求
本文對您有任何幫助嗎?
(剩餘 1500 個字元)

社群新增項目

新增
© 2013 Microsoft. 著作權所有,並保留一切權利。