Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ViewBox not sizing to parent correctly.

$
0
0

Hi,

Please follow me a bit first :) (all code is simplified):

I have a tilegrid custom layout panel in which tiles are shown. When i define the Tile's template like this (again, simplified):

<Button x:Name="PART_OpenTileButton" Style="DynamicResource EmptyButtonStyle}" Foreground="TemplateBinding Foreground}"><Grid><Button>Hello world!</Button></Grid></Button>

Result:

The measuring by the custom layout control seems to work ok, but when I do this:

<Button x:Name="PART_OpenTileButton" Style="DynamicResource EmptyButtonStyle}" Foreground="TemplateBinding Foreground}"><Grid><Viewbox><Button>Hello world!</Button></Viewbox></Grid></Button>

I suddenly get this:

So, the ViewBox is taking the wrong size, although other controls do it right. It seems to take the Width of the entire TileGrid, not just one tile.

If i use one of my Tiles outside of my custom layout control (eg directly in a Grid), the ViewBox works correctly. So, I guess my layout panel is doing something wrong but it must be subtle since only the Viewbox has trouble with it. 

Its code:

protected override Size MeasureOverride(Size availableSize)
        {
            foreach (UIElement child in InternalChildren)
            {
                child.Measure(availableSize);
            }
            return double.IsPositiveInfinity(availableSize.Width) ? new Size() : availableSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            if (InternalChildren.Count == 0) return finalSize;

            // ...
            // pure math code calculating several variables

            foreach (UIElement child in InternalChildren)
            {
                var arrangeRectangle = GetArrangeRectangle(child, spacing, tileWidth, tileHeight);
                
                child.Arrange(arrangeRectangle); // pass the small tile-rectangle to this tile.

                var newHeight = arrangeRectangle.Y + arrangeRectangle.Height;

                if (newHeight > totalHeight)
                {
                    totalHeight = newHeight;
                }
            }

            return new Size(finalSize.Width, totalHeight > finalSize.Height ? totalHeight : finalSize.Height);
        }


Viewing all articles
Browse latest Browse all 18858

Trending Articles