问题
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