如何从代码向XAML/WINRT网格添加项目 [英] How to add items to XAML/WINRT Grid from Code
问题描述
因此,我正在尝试创建一个网格,该网格可以根据框架打开的时间由0到X个项目动态填充.
So, I am trying to create a grid that can be dynamically populated by 0 to X items depending on when the Frame is open.
我阅读了有关如何创建网格并将其添加到代码中的MSDN文章: http://msdn.microsoft.com/en-us/library/ms752271.aspx
I read the MSDN article on how to create a grid and add to it in code: http://msdn.microsoft.com/en-us/library/ms752271.aspx
但是我想在XAML中创建网格并在代码中添加到网格中.像这样:
Howeverm I want to create the Grid in the XAML and add to it in the code. Something like this:
XAML:
<Grid x:Name="ManagePhotosContent" Grid.Row="1" Visibility="Visible">
<!-- to be filled in by code -->
</Grid>
在C#代码中,我正在执行以下操作:
In C# code i am doing something like this:
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 ="*".我在这里做错什么了吗?
However, SetRow is not an available function or property of the ManagePhotosContent grid that I created in XAML. Further, I am unable to see how to set up the RowDefinition in code to be of Height="*". Am I doing something wrong here?
推荐答案
替换
ManagePhotosContent.SetRow(textBlock, i);
与
Grid.SetRow(textBlock, i);
SetRow
是Grid
类的静态方法,因此不能在对象实例上使用.设置 RowDefinition.Height
使用:
SetRow
is a static method of Grid
class and therefore cannot be used on object instance. To set RowDefinition.Height
use that:
row.Height = new GridLength(1, GridUnitType.Star);
这篇关于如何从代码向XAML/WINRT网格添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!