Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Grid Column/Row width/Height dynamically

Tags:

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 ?

like image 582
Welsh King Avatar asked Mar 15 '12 13:03

Welsh King


People also ask

How do you set Ag grid dynamically height?

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/ ...

How do I set row height in grid?

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.

How do you reduce row height in Ag grid?

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 ...

How do you change the width of Ag grid?

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.


2 Answers

You could specify it like this:

For auto sized columns:

GridLength.Auto 

For star sized columns:

new GridLength(1,GridUnitType.Star) 
like image 70
ionden Avatar answered Oct 27 '22 08:10

ionden


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!

like image 33
Jamaxack Avatar answered Oct 27 '22 08:10

Jamaxack