6

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

        }
4

1 に答える 1

8

TextBlock をグリッドに追加することはありません。Grid に名前を付け (例: x:Name="myGrid")、myGrid.Children.Add(txt) を呼び出す必要があります。

于 2012-06-10T15:29:47.063 に答える