How do I construct this piece of XAML programatically?
<Grid Name="gridMarkets"> <Grid.RowDefinitions> <RowDefinition Height="10" /> <RowDefinition Height="*" MinHeight="16" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="10" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> </Grid>
Is it any elegant solution for parse and construct controls dynamically?
I was trying to do something:
RowDefinition newRow = new RowDefinition(); newRow.Height = new GridLength(10); newGrid.RowDefinitions.Add(newRow);
But how do I assign a * sign?
You can use the
Grid.Star unit type:
newRow.Height = new GridLength(1, GridUnitType.Star);
You can also use the
XamlReader object to convert XAML strings into UI objects from code-behind, although I usually prefer to manually create objects like how you are creating them.