The data type of columndefinition. width is gridlength, so you can write it:
Column1.width = new gridlength (1, gridunittype. Star); // column1 is an instance.
If columndefinition width = "Auto", you can write it
Column1.width = gridlength. Auto;
Do not confuse here. gridlength. Auto is a structure, while new gridlength (1, gridunittype. Star) is a class.
Below is a complete layout code, all completed using C # code (Exercise)
Public partial class mainwindow: Window
{
Public mainwindow ()
{
Initializecomponent ();
// Display grid
Grid1.showgridlines = true;
// Line
Rowdefinition row1 = new rowdefinition ();
Rowdefinition row2 = new rowdefinition ();
// Column
Columndefinition column1 = new columndefinition ();
Columndefinition column2 = new columndefinition ();
// Use the * layout. When the value is 1, when the * value is 2, 2 * indicates that the width of the second column is twice the width of the first column.
Column1.width = new gridlength (1, gridunittype. Star );
Column2.width = new gridlength (2, gridunittype. Star );
Button button1 = new button ();
Button button2 = new button ();
Button button3 = new button ();
Button button4 = new button ();
// Add rows and columns to the grid panel
Grid1.rowdefinitions. Add (row1 );
Grid1.rowdefinitions. Add (row2 );
Grid1.columndefinitions. Add (column1 );
Grid1.columndefinitions. Add (column2 );
// Add the button to the grid panel
Grid1.children. Add (button1 );
Grid1.children. Add (button2 );
Grid1.children. Add (button3 );
Grid1.children. Add (button4 );
Button1.content = "1 ";
Button2.content = "2 ";
Button3.content = "3 ";
Button4.content = "4 ";
// Set the cell where each button is located
Grid. setrow (button1, 0 );
Grid. setcolumn (button1, 0 );
Grid. setrow (button2, 0 );
Grid. setcolumn (button2, 1 );
Grid. setrow (button3, 1 );
Grid. setcolumn (button3, 0 );
Grid. setrow (button4, 1 );
Grid. setcolumn (button4, 1 );
}
}