#1,201 – How to Share Star Sized Column Sizes
March 4, 2017 2 Comments
You can use the SharedSizeGroup attribute to share column sizes across different grids. You can’t normally share sizes of star-sized columns across multiple grids, however, since star sizing works only in the context of the current grid.
Below is an example of how to get a star sized column to be the same size as a star sized column in a different grid. In the example, we have two grids. In the first, we have two auto-sized TextBlocks and then a star-sized TextBox that takes up the remaining space. In the second grid, we have just a TextBlock and a TextBox, but we want the TextBox to be the same size as the one in the first grid. Using SharedSizeGroup on the TextBox columns won’t work, since the columns would then get auto-sized.
The solution is to set up a SharedSizeGroup on every column except for the TextBox columns and to add a dummy third column in the second grid. The TextBox columns will then each independently use star sizing and end up the same size.
<StackPanel Margin="15" Grid.IsSharedSizeScope="True"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="A"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto" SharedSizeGroup="B"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="Col 1"/> <TextBox Grid.Column="1" /> <TextBlock Grid.Column="2" Text="3rd column here"/> </Grid> <Separator Margin="0,20"/> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="A"/> <ColumnDefinition /> <ColumnDefinition SharedSizeGroup="B"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="1"/> <TextBox Grid.Column="1"/> </Grid> </StackPanel>
Pingback: Dew Drop - March 6, 2017 (#2434) - Morning Dew
The great thing about ‘Grid.IsSharedSizeScope’ is that it is a hierarchical setting. By this, I mean that any grid within the scope, child elements, has access to the scope. So if the parent Grid with a SharedSizeGroup contains a UserControl with a Grid using the SharedSizeGroup, the sizing will be applied.