我有兩個類[適用於此問題]。第一個,XYAxes2
,延伸FrameworkElement
。它覆蓋了MeasureOverride
和ArrangeOverride
,試圖將它的獨生子XAxis
(也擴展爲FrameworkElement
)放置在所需位置。FramworkElement不安置孩子FramworkElement
XYAxes2
(Parent):的
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
在哪裏實際設置(顯然他們不是由我的代碼設置)?
DesirableSize通常在Mesasure傳遞後設置,ActualWidth/Height在控件獲取Arranged/Rendered(?)後設置。通過它的聲音,你需要調用Measure(double.PositiveInfinity,doublePositiveInfinity);畢竟我不認爲Draw *會適合Measure/Arrange佈局範式。 – 2011-11-08 20:01:41