我有两个课程[适用于这个问题]。第一个XYAxes2
,扩展FrameworkElement
。它覆盖MeasureOverride
并ArrangeOverride
试图将其唯一的孩子XAxis
(也扩展FrameworkElement
)放置在所需的位置。
XYAxes2
(家长):
Scale XAxis =//...
protected override Size MeasureOverride( Size availableSize ) {
XAxis.Measure( availableSize );
return (Size)availableSize;
}
protected override Size ArrangeOverride( Size finalSize ) {
XAxis.Arrange( new Rect( 50, 50, 100, 200 ) );
return finalSize;
}
Scale
是一个自定义绘制的组件。
Scale
(孩子):
protected override void OnRender( DrawingContext cx ) {
ValidateTicks();
if (Orientation == Orientation.Horizontal) {
cx.DrawLine( Pen, new Point( -.5, .5 ), new Point( ActualWidth - .5, .5 ) );
foreach (double tick in _ticks.Keys) {
double val = ScaleTransformCallback( tick );
double x = -0.5 + (int)((val - MinValue) * ActualWidth / (MaxValue - MinValue));
cx.DrawLine( Pen, new Point( x, .5 ), new Point( x, TickLength - .5 ) );
FormattedText txt = _ticks[tick];
cx.DrawText( txt, new Point( x - txt.Width / 2, TickLength + TextMargin ) );
}
} else {
double Left = maxTextWidth + 2 * TextMargin;
double Right = this.TickLength + Left;
cx.DrawLine( Pen, new Point( Right - .5, +.5 ), new Point( Right - .5, ActualHeight + .5 ) );
foreach (double tick in _ticks.Keys) {
double val = ScaleTransformCallback( tick );
double y = -0.5 + ActualHeight - (int)((val - MinValue) * ActualHeight / (MaxValue - MinValue));
cx.DrawLine( Pen, new Point( Right - .5, y ), new Point( Left - .5, y ) );
FormattedText txt = _ticks[tick];
cx.DrawText( txt, new Point( Left - txt.Width - TextMargin, y - txt.Height / 2 ) );
}
}
}
调试时,ActualWidth
在此函数中始终是父级的宽度,而不是ArrangeOverride
100 设置的宽度。但是,子级被剪裁到 指定的区域ArrangeOverride
。
我在这些功能之一中做错了吗?
编辑:通过插入XAxis.Measure(100,200)
,ArrangeOverride
问题显然得到了解决。当然,每次安排无效时都不需要调用Measure
(这意味着InvalidateArrange
需要隐式调用InvalidateMeasure
)。任何人都可以阐明这背后的原因吗?
有谁知道实际设置在哪里DesiredSize
(ActualWidth/Height
显然它们不是由我的代码设置的)?