更新 : 2007 年 11 月
使用例
この例では、Panel 要素の既定のレイアウト動作をオーバーライドして、Panel から派生したカスタム レイアウト要素を作成する方法を示します。
この例では、PlotPanel という単純なカスタム Panel 要素を定義します。この要素は、ハードコーディングされた x 座標および y 座標に従って子要素を配置します。この例では、x および y をどちらも 50 に設定します。そのため、すべての子要素は x 座標および y 座標上のこの位置に配置されます。
カスタムの Panel 動作を実装するために、この例では MeasureOverride メソッドと ArrangeOverride メソッドを使用します。各メソッドは、子要素の配置とレンダリングに必要な Size データを返します。
Public Class PlotPanel
Inherits Panel
'Override the default Measure method of Panel.
Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
Dim childSize As Size = CType(availableSize, Size)
For Each child As UIElement In InternalChildren
child.Measure(childSize)
Next
Return MyBase.MeasureOverride(availableSize)
End Function
Protected Overrides Function ArrangeOverride(ByVal finalSize As System.Windows.Size) As System.Windows.Size
For Each child As UIElement In InternalChildren
Dim x As Double = 50
Dim y As Double = 50
child.Arrange(New Rect(New System.Windows.Point(x, y), child.DesiredSize))
Next
Return MyBase.ArrangeOverride(finalSize)
End Function
End Class
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
}
}
サンプル全体については、「簡単なカスタム パネルの作成のサンプル」を参照してください。