简体   繁体   English

动态地向网格添加文本块

[英]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. 我的网格有2行,2列,我想动态地将文本块添加到第一行,第二列。

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

        }

You are never adding your TextBlock to the grid. 您永远不会将TextBlock添加到网格中。 You should name your Grid (eg. x:Name="myGrid") and call myGrid.Children.Add(txt) at some point. 你应该命名你的网格(例如x:Name =“myGrid”)并在某个时候调用myGrid.Children.Add(txt)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM