WPF Container that resizes children

411 Views Asked by At

I need to find a WPF Container control where when layed out with a fixed size, can adequately resize children controls so that the size of each child control is reduced as new items are created/added in/to the container.

So assume this control would have some tile like children controls, assume at one point my container has 4 tiles - 16X16 that fill the entire control. When I add the next row of tiles, I want the existing 4 tiles to shrink down to say 12X12, I don't want the size of container to accommodate as I don't ever want to get to a point where my users are scrolling across the container.

Please let me know if anyone has any ideas...?

2

There are 2 best solutions below

0
On

something like this would probably work

<ItemsControl >
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid  />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <Grid Width="50" Height="70" Background="Red" />
    <Grid Width="50" Height="70" Background="Orange" />
    <Grid Width="50" Height="70" Background="Blue" />
    <Grid Width="50" Height="70" Background="Green" />
    <Grid Width="50" Height="70" Background="Yellow" />
    <Grid Width="50" Height="70" Background="Brown" />
    <Grid Width="50" Height="70" Background="Violet" />
    <Grid Width="50" Height="70" Background="AntiqueWhite" />
    <Grid Width="50" Height="70" Background="DarkGoldenrod" />
    <Grid Width="50" Height="70" Background="Firebrick" />
    <Grid Width="50" Height="70" Background="Thistle" />
</ItemsControl>
0
On

I can't think of any way to do it with the stock panels, but it would be pretty simple to write your own panel to arrange in the optimal grid size. Maybe you could subclass UniformGrid to set the row/columns to an appropriate value when a child is added.

Once you have that you can just stick the whole thing in a ViewBox to do the scaling automatically.