|
本文章是由人工翻譯。 將指標移到文章內的文字上方即可查看原文。
|
譯文
原文
|
FrameworkElement.MeasureOverride 方法
組件: PresentationFramework (在 PresentationFramework.dll 中)
參數
- availableSize
- 型別:System.Windows.Size
這個項目可以提供給子項目使用的大小。 您可以將 Infinity 指定為值,這個值表示項目大小可以調整至內容使用的任何大小。
反覆查看項目的特定子系集合,這個集合是配置的一部分,會在每個子項目上呼叫 Measure。 立即在子項目上取得 DesiredSize (如此一來會在呼叫 Measure 之後設定為屬性)。 根據子項目的測量,計算父系最終所需的大小。
重要事項 |
|---|
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 } }
Windows 7, Windows Vista SP1 (含) 以後版本, Windows XP SP3, Windows Server 2008 (不支援伺服器核心), Windows Server 2008 R2 (SP1 (含) 以後版本支援伺服器核心), Windows Server 2003 SP2
.NET Framework 並不支援各種平台的所有版本。如需支援的版本的清單,請參閱.NET Framework 系統需求。
重要事項