방법: 사용자 지정 패널 요소 만들기
업데이트: 2007년 11월
예제
이 예제에서는 Panel 요소의 기본 레이아웃 동작을 재정의하고 Panel에서 파생된 사용자 지정 레이아웃 요소를 만드는 방법을 보여 줍니다.
예제에서는 두 개의 하드 코드된 x 및 y 좌표에 따라 자식 요소를 배치하는 PlotPanel이라는 간단한 사용자 지정 Panel 요소를 정의합니다. 이 예제에서 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
}
}
전체 샘플을 보려면 간단한 사용자 지정 패널 만들기 샘플을 참조하십시오.