Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a textblock to grid dynamically

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

        }
like image 644
Luis Valencia Avatar asked Jun 10 '12 13:06

Luis Valencia


1 Answers

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.

like image 72
Filip Skakun Avatar answered Oct 01 '22 05:10

Filip Skakun