Question about WPF FrameworkElement.ArrangeOverride(Size finalSize) method
When creating a custom Panel
, we need to override FrameworkElement.MeasureOverride(Size availableSize)
and FrameworkElement.ArrangeOverride(Size finalSize)
to organise our custom layout of items. The MSDN FrameworkElement.ArrangeOverride Method page states that the return value of type System.Windows.Size
should be "The actual size used".
However, if the actual size used by the items is smaller than the size of the container, then the item 开发者_如何学JAVAcontent is centred in the container. By this I mean that a bounds rectangle of the size returned contains all the items and is centred in the container.
I tried setting the HorizontalContentAlignment
and VerticalContentAlignment
properties on the ListBox
that uses my custom Panel
, but it made no difference. Strangely though, if I return the unadjusted finalSize
input variable instead of the actual size used, then my items appear just as expected and the custom Panel
works fine. Can anyone enlighten me as to why this is? Also, is it possible to position the bounds rectangle containing the items when returning the actual size?
The size returned by MeasureOverride
and ArrangeOverride
is the size of the element doing the returning, not the size of its content. Just give your panel a background and that's the size we're talking about.
You can only position the top-left of a panel (or any other element) with the cooperation its parent container. In general your only two choices are Margin="x,y,0,0"
to provide positive or negative offsets relative to the top-left the container gives you, or to use a Canvas
as a parent container and use Canvas.Top
and Canvas.Left
.
精彩评论