c# - 动态添加文本 block 到网格

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

我的网格有 2 行、2 列,我想动态添加一个文本 block 到第一行、第二列。

这是我的代码,它没有抛出异常但也没有显示任何内容

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

        }

最佳答案

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

关于c# - 动态添加文本 block 到网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10968922/

相关文章:

c# - 根据数据库条件填充 DataGridView

c# - 如何在 ASP.NET MVC 中获取当前用户

c# - 如何更改默认的 Xaml TextBox 字体大小

windows-runtime - Windows.Web.Http.HttpClient超时选项

c# - Entity Framework : Check all relationships of an entity for foreign key use

c# - 如何定义一个像 'checked' 这样的关键字的属性?

silverlight - 如何调试 XamlParseException?

c# - 具有统一 BorderThickness 的边框无法绑定(bind) BorderBrush 颜色

c# - 使用 DataContractSerializer 和 DataProtectionProvider 序列化和加密对象

windows-runtime - 图像源的 URI 格式