Reputation: 33978
My grid has 2 rows, 2 columns, and I want to add a textblock dynamically to first row, second column.
This is my code, which doesnt throw an exception but it doesnt show anything
<Grid HorizontalAlignment="Left" Height="768" VerticalAlignment="Top" Width="1366">
<Grid.RowDefinitions>
<RowDefinition Height="150"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
</Grid>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
TextBlock txt = new TextBlock();
txt.Width = 200;
txt.Height = 100;
txt.Foreground = new SolidColorBrush(Colors.Yellow);
var location = await InitializeLocationServices();
txt.Text = location;
Grid.SetRow(txt, 0);
Grid.SetColumn(txt, 1);
}
Upvotes: 7
Views: 11466
Reputation: 31724
You are never adding your TextBlock to the grid. You should name your Grid (eg. x:Name="myGrid") and call myGrid.Children.Add(txt) at some point.
Upvotes: 9