I need to create a WPF grid dynamically from code behind. This is going okay and I can do it so that I set the content widths but what I need to do is set them so that when i resize the window the controls are re sized dynamically
var col = new ColumnDefinition(); col.Width = new System.Windows.GridLength(200); grid1.ColumnDefinitions.Add(col);
This will produce XAML
<Grid.ColumnDefinitions> <ColumnDefinition Width="200"></ColumnDefinition> </Grid.ColumnDefinitions>
But what I need is to use a * or question mark ie.
<Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions>
But the WidthValue does not support a * or question mark a when creating from code behind ?
You can use ag-grid feature AutoHeight = true in the column Configuration. or if you want to calculate the height dynamically you should use getRowHeight() callback and create DOM elements like div and span , and add your text in it, then the div will give the OffsetHeight for it then you wont see any cropping of data/ ...
To change the row height for the whole grid, set the property rowHeight to a positive number. For example, to set the height to 50px, do the following: const gridOptions = { rowHeight: 50, // other grid options ... } Changing the property will set a new row height for all rows, including pinned rows top and bottom.
To change the row height so that each row can have a different height, implement the getRowHeight(params) callback. For example, to set the height to 50px for all group rows and 25px for all other rows, do the following: <ag-grid-angular [getRowHeight]="getRowHeight" /* other grid options ...
Under normal usage, your application should set the width and height of the grid using CSS styles. The grid will then fit the width you provide and use scrolling inside the grid to allow all rows and columns to be viewed.
You could specify it like this:
For auto sized columns:
GridLength.Auto
For star sized columns:
new GridLength(1,GridUnitType.Star)
There is 3 types of setting Width to Grid ColumnDefinitions:
For Percentage Column:
yourGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
In xaml:
<ColumnDefinition Width="1*"/>
For Pixel Column
yourGrid.ColumnDefinitions[0].Width = new GridLength(10, GridUnitType.Pixel); yourGrid.ColumnDefinitions[0].Width = new GridLength(10);
In xaml:
<ColumnDefinition Width="10"/>
For Auto Column
yourGrid.ColumnDefinitions[0].Width = GridLength.Auto;
In xaml:
<ColumnDefinition Width="Auto"/>
Hope it helps!
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