2011-11-08 17 views
2

我有兩個類[適用於此問題]。第一個,XYAxes2,延伸FrameworkElement。它覆蓋了MeasureOverrideArrangeOverride,試圖將它的獨生子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)。任何人都可以闡明這背後的原因嗎?

有誰知道DesiredSizeActualWidth/Height在哪裏實際設置(顯然他們不是由我的代碼設置)?

+0

DesirableSize通常在Mesasure傳遞後設置,ActualWidth/Height在控件獲取Arranged/Rendered(?)後設置。通過它的聲音,你需要調用Measure(double.PositiveInfinity,doublePositiveInfinity);畢竟我不認爲Draw *會適合Measure/Arrange佈局範式。 – 2011-11-08 20:01:41

回答

1

我爲此使用的解決方案是在MeasureOverride函數中使用多遍。我一直測量不同的尺寸,直到確定合適的整體尺寸。

通過經由.NET 4.0源代碼讀取,看來: -

  • DesiredSizeUIElement.Measure功能設定爲的MeasureOverride的 返回值。

  • ActualWidth/ActualHeightRenderSize)由 FrameworkElement.ArrangeCore設定爲任何值從 ArrangeOverride返回似乎沒有被舍入應用於任何錯誤檢查或 。

我希望這可以幫助其他人。