我怎样才能dynamic地添加一个RowParameters到GridPanelTemplate?
我有一个固定数量的列和未知数量的行的网格。 行数在构造函数中设置一次。
<ItemsControl Name="myItemsControl" ItemsSource="{Binding Cells}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid Name="myGrid"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <!-- some rows should be added here --> </Grid.RowDefinitions> </Grid> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style.../> </ItemsControl.ItemContainerStyle> </ItemsControl> 我已经尝试了下面这行代码,但是它返回null:
 object obj = myItemsControl.Template.FindName("myGrid", myItemsControl); 
我怎样才能在代码中添加行到“myGrid”?
 在设置或更改这些属性时,可以使用附加属性来修改RowDefinitions和ColumnDefinitions 。 
 它可以让你像这样写你的Grid : 
 <Grid local:GridHelpers.RowCount="{Binding MaxGridRow}" local:GridHelpers.ColumnCount="3" /> 
 然后从ViewModel暴露一个属性,它返回Cells集合中最大的行号。 
你可以在我的博客上find这些属性的详细实现。
 var rowDefinition = new RowDefinition(); rowDefinition.Height = GridLength.Auto; grid.RowDefinitions.Add(rowDefinition); 
 只要使用myItemsControl.GetTemplatedChild("myGrid") 。 另外,在开始使用上述expression式之前,您必须加载控件。