【问题标题】:How to add items to XAML/WINRT Grid from Code如何将项目从代码添加到 XAML/WINRT 网格
【发布时间】:2013-05-27 02:16:49
【问题描述】:

因此,我正在尝试创建一个网格,该网格可以根据 Frame 的打开时间由 0 到 X 个项目动态填充。

我阅读了有关如何创建网格并在代码中添加到其中的 MSDN 文章: http://msdn.microsoft.com/en-us/library/ms752271.aspx

但是,我想在 XAML 中创建 Grid 并在代码中添加它。像这样的:

XAML:

<Grid x:Name="ManagePhotosContent" Grid.Row="1" Visibility="Visible">
<!-- to be filled in by code -->
</Grid>

在 C# 代码中,我正在做这样的事情:

rowDef = new RowDefinition();
ManagePhotosContent.RowDefinitions.Add(rowDef);
textBlock = new TextBlock();

// i is an incrementer
textBlock.Text = string.Format("The is iteration {0}, i);
ManagePhotosContent.SetRow(textBlock, i);

但是,SetRow 不是我在 XAML 中创建的 ManagePhotosContent 网格的可用函数或属性。此外,我无法看到如何将代码中的 RowDefinition 设置为 Height="*"。我在这里做错了吗?

【问题讨论】:

  • SetRow 不是 Grid 上的静态方法吗?

标签: xaml visual-studio-2012 grid windows-runtime


【解决方案1】:

替换

ManagePhotosContent.SetRow(textBlock, i);

Grid.SetRow(textBlock, i);

SetRowGrid 类的静态方法,因此不能用于对象实例。要设置RowDefinition.Height,请使用:

row.Height = new GridLength(1, GridUnitType.Star);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多