I create a GridSplitter
across the 3 rows that I have in my grid like this:
<GridSplitter Grid.Row="0" Grid.Column="1" Background="Yellow" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" ResizeDirection="Columns" Grid.RowSpan="3" ...
However, it's conceivable that I might add another row to my grid at a later stage, and I don't really want to go back and update all of my rowspans.
My first guess was Grid.RowSpan="*"
, but that doesn't compile.
You can span across multiple rows and columns using the Grid. RowSpan and Grid. ColumnSpan attached properties. The default value for both these properties is 1. The Grid will attempt to assign as many row spans or column spans as it can up to the amount specified by the Grid.
A GridSplitter is a divider that divides a Grid into two sections. A GridSplitter allows us to resize rows or columns in a Grid by dragging the GridSplitter Bar. An example of a GridSplitter is the Windows Explorer.
A simple solution:
<!-- RowSpan == Int32.MaxValue --> <GridSplitter Grid.Row="0" Grid.Column="1" Grid.RowSpan="2147483647" />
You can bind to the RowDefinitions.Count but would need to update the binding when adding rows manually.
Edit: Only semi-manually in fact
Xaml:
<StackPanel Orientation="Vertical"> <Grid Name="GridThing"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.Children> <Button Content="TopRight" Grid.Row="0" Grid.Column="1"/> <Button Content="LowerRight" Grid.Row="1" Grid.Column="1"/> <Button Content="Span Rows" Name="BSpan" Grid.RowSpan="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=RowDefinitions.Count, Mode=OneWay}"/> </Grid.Children> </Grid> <Button Click="Button_Click" Content="Add Row" /> </StackPanel>
Code:
private void Button_Click(object sender, RoutedEventArgs e) { GridThing.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(20) }); foreach (FrameworkElement child in GridThing.Children) { BindingExpression exp = child.GetBindingExpression(Grid.RowSpanProperty); if (exp != null) { exp.UpdateTarget(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With