动态地向网格添加文本块

时间:2012-06-10 13:02:29

标签: c# xaml windows-runtime windows-store-apps winrt-xaml

我的网格有2行,2列,我想动态地将文本块添加到第一行,第二列。

这是我的代码,它不会抛出异常,但它不会显示任何内容

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

        }

1 个答案:

答案 0 :(得分:7)

您永远不会将TextBlock添加到网格中。你应该为你的网格命名(例如x:Name =“myGrid”)并在某个时候调用myGrid.Children.Add(txt)。

相关问题