Reputation: 954
I have a problem. I want to change the RowHeight for each row in the Grid, so I gave every row a name like this:
<Grid.RowDefinitions>
<RowDefinition Height="40" x:Name="Row0_Height"/>
<RowDefinition Height="*" x:Name="Row1_Height"/>
<RowDefinition Height="*" x:Name="Row2_Height"/>
<RowDefinition Height="30" x:Name="Row3_Height"/>
</Grid.RowDefinitions>
But how can I set those values using c# code?
Upvotes: 1
Views: 1548
Reputation: 6971
I am not sure you can do this at xaml. Or At least I don't know. The easier way will be do this at code behind and creating a grid or setting grid up with new row definition.
Something like this will work:
MyGrid.RowDefinitions = new RowDefinitionCollection();
MyGrid.ColumnDefinitions = new ColumnDefinitionCollection();
for (int i = 0; i< 5; i++)
{
MyGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
MyGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new G GridLength(1, GridUnitType.Star) });
}
Or you can try to bind the height :
<Grid.RowDefinitions>
<RowDefinition Height="{Binding RowSize, Mode=TwoWay}" />
</Grid.RowDefinitions>
And Model:
private float _rowSize = 230;
public float RowSize
{
get {
return _rowSize ;
}
}
Upvotes: 1
Reputation: 3387
the C# equivalent of this code would be :-
Row0_Height.Height = new GridLength(40);
Row1_Height.Height = new GridLength(1, GridUnitType.Star);
Row2_Height.Height = new GridLength(1, GridUnitType.Star);
Row3_Height.Height = new GridLength(30);
Upvotes: 0