Have two controls fill the MainWindow and resize automatically
-
I have four controls in a horizontal row. The first and third control shouöd automatically resize and fill the available space when resizing the MainWindow. The second and forth controls have a static width and I don't want them to change horizontally in size. All four controls should fill vertically. I have almost solved it with an UniformGrid as container for the four controls.
Run the example and resize the window. * The blue and red rectangle will resize appropriately horizontally and fill vertically. * The green and yellow rectangle only fills vertically. But I cannot understand how to fill/remove the white spaces to the left of all rectangles. Any suggestions?
-
I have four controls in a horizontal row. The first and third control shouöd automatically resize and fill the available space when resizing the MainWindow. The second and forth controls have a static width and I don't want them to change horizontally in size. All four controls should fill vertically. I have almost solved it with an UniformGrid as container for the four controls.
Run the example and resize the window. * The blue and red rectangle will resize appropriately horizontally and fill vertically. * The green and yellow rectangle only fills vertically. But I cannot understand how to fill/remove the white spaces to the left of all rectangles. Any suggestions?
The columns in a
UniformGrid
will always be the same width. Since you have four columns, each column will be ¼ of the width of the window. The controls with a defined width are aligned in the centre of the column, which is why you're getting white-space around them. The obvious option would be to use a standardGrid
. This would require adding column definitions, and setting theGrid.Column
attached property on each child control.<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions><Rectangle Fill="Blue" Grid.Column="0" /> <Rectangle Fill="Lime" Width="20" Grid.Column="1" /> <Rectangle Fill="Red" Grid.Column="2" /> <Rectangle Fill="Yellow" Width="20" Grid.Column="3" />
</Grid>
Another alternative would be to combine the
UniformGrid
with aDockPanel
:<UniformGrid Columns="2">
<DockPanel>
<Rectangle Fill="Lime" Width="20" DockPanel.Dock="Right" />
<Rectangle Fill="Blue" />
</DockPanel>
<DockPanel>
<Rectangle Fill="Yellow" Width="20" DockPanel.Dock="Right" />
<Rectangle Fill="Red" />
</DockPanel>
</UniformGrid>
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The columns in a
UniformGrid
will always be the same width. Since you have four columns, each column will be ¼ of the width of the window. The controls with a defined width are aligned in the centre of the column, which is why you're getting white-space around them. The obvious option would be to use a standardGrid
. This would require adding column definitions, and setting theGrid.Column
attached property on each child control.<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions><Rectangle Fill="Blue" Grid.Column="0" /> <Rectangle Fill="Lime" Width="20" Grid.Column="1" /> <Rectangle Fill="Red" Grid.Column="2" /> <Rectangle Fill="Yellow" Width="20" Grid.Column="3" />
</Grid>
Another alternative would be to combine the
UniformGrid
with aDockPanel
:<UniformGrid Columns="2">
<DockPanel>
<Rectangle Fill="Lime" Width="20" DockPanel.Dock="Right" />
<Rectangle Fill="Blue" />
</DockPanel>
<DockPanel>
<Rectangle Fill="Yellow" Width="20" DockPanel.Dock="Right" />
<Rectangle Fill="Red" />
</DockPanel>
</UniformGrid>
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer