Add a textblock to grid dynamically

c#, windows-runtime, windows-store-apps, winrt-xaml, xaml

Solution

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.

Problem

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); } ```

Original source

Related problems