How to add items to XAML/WINRT Grid from Code

一曲冷凌霜 提交于 2019-12-25 07:16:55

问题


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.

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

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>

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

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?


回答1:


replace

ManagePhotosContent.SetRow(textBlock, i);

with

Grid.SetRow(textBlock, i);

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


来源:https://stackoverflow.com/questions/16761810/how-to-add-items-to-xaml-winrt-grid-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!