I want to programmatically configure a wpf grid.
I want to be able to set a grid with 2 columns, the first taking up 20% of available space, the second taking up 80%. In xaml I would use the * operator but I cant work out how to do this programmatically.
In Xaml I would do:
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition width="20*" /> <ColumnDefinition width="80*" /> </Grid>
In code I want to do:
Grid grid = new Grid(); grid.ColumnDefinitions.Add( new ColumnDefinition(20*) ); grid.ColumnDefinitions.Add( new ColumnDefinition(80*) );
Please could someone advise.
RowDefinitions. Add(gridRow3); Once rows and columns are added to Grid, you can add any contents to Grid cells by using SetRow and SetColumn methods. SetRow and SetColumn methods take first parameter as the control name and second parameter as row number and column number respectively.
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.
Try setting your Grid's MaxWidth to 620 (the sum of your MaxWidths) or something similar. Your Grid will stretch to fill your max size, but still be small enough for the HorizontalAlignment =Center to be observable.
Grid grid = new Grid(); ColumnDefinition c1 = new ColumnDefinition(); c1.Width = new GridLength(20, GridUnitType.Star); ColumnDefinition c2 = new ColumnDefinition(); c2.Width = new GridLength(80, GridUnitType.Star); grid.ColumnDefinitions.Add(c1); grid.ColumnDefinitions.Add(c2);
Suppose you have some buttons (aligned horizontally) in a page and need to hide / show certain ones depending on some status.
<Grid HorizontalAlignment="Center" Grid.Column="1" Width="340" VerticalAlignment="Center" Background="Transparent"> <Grid.ColumnDefinitions> <ColumnDefinition Width="2*" x:Name="colOne"></ColumnDefinition> <ColumnDefinition Width="0" x:Name="colTwo"></ColumnDefinition> <ColumnDefinition Width="0" x:Name="colThree"></ColumnDefinition> <ColumnDefinition Width="0" x:Name="colFour"></ColumnDefinition> </Grid.ColumnDefinitions> <Button x:Name="btnOne" Grid.Column="0" Height="50" Width="50" Content="One" Cursor="Hand" /> <Button x:Name="btnTwo" Grid.Column="1" Height="50" Width="50" Content="Two" Cursor="Hand" /> <Button x:Name="btnThree" Grid.Column="2" Height="50" Width="50" Content="Thre" Cursor="Hand" /> <Button x:Name="btnFour" Grid.Column="3" Height="50" Width="50" Content="Four" Cursor="Hand" /> </Grid>
Here btnOne will be visible in the page when executed. btnOne will also be aligned in the center. Now if we want Three and Four to be displayed and One to be hidden when clicked on One, we can use this code:
private void btnOne_Click(object sender, RoutedEventArgs e) { SetGridColWidth(colOne, false); SetGridColWidth(colThree, true); SetGridColWidth(colFour, true); } private void SetGridColWidth(ColumnDefinition column, bool show) { if (show) column.Width = new GridLength(2, GridUnitType.Star); else column.Width = new GridLength(0); }
You can toggle between visibility of any button at runtime.
Hope this helps someone!
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