Consider this simple Grid:
<Grid x:Name="gridTest" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
In code-behind, I add this code:
public MainWindow()
{
InitializeComponent();
Label labTest = new Label { Content = "Test Label" };
gridTest.Children.Add(labTest);
}
Note the label is on top left of the grid. Its ok because I dont set the Grid.Col and Grid.Row (using Grid.SetColumn() and Grid.SetRow())
Now, check the next example:
public MainWindow()
{
InitializeComponent();
Label labTest = new Label { Content = "Test Label", Height = 30, Width = 70 };
gridTest.Children.Add(labTest);
}
I simply added a height and width for the label. But the result is:
Now, the label acts like centralized in the first cell of the grid although I didnt specify Grid rows or cols for the label! My questions:
- Why this happens? Is it "by design" or its a bug?
- What can I do to the label in the second example to act like in the first, i.e. appears on topmost and leftmost (0,0) location of the grid when added to grid.Children collection?