我如何动态地将一个RowDefinition添加到ItemsPanelTemplate中的网格? [英] How can I dynamically add a RowDefinition to a Grid in an ItemsPanelTemplate?
问题描述
我有一个固定数量的列和未知行数的网格。
< ItemsControl Name =myItemsControlItemsSource ={Binding Cells} >
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
< Grid Name =myGrid>
< Grid.ColumnDefinitions>
< ColumnDefinition />
< ColumnDefinition />
< ColumnDefinition />
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
<! - 应该在这里添加一些行 - >
< /Grid.RowDefinitions>
< / Grid>
< / ItemsPanelTemplate>
< /ItemsControl.ItemsPanel>
< ItemsControl.ItemContainerStyle>
< Style ... />
< /ItemsControl.ItemContainerStyle>
< / ItemsControl>
我尝试了下面这行代码,但它返回null:
object obj = myItemsControl.Template.FindName(myGrid,myItemsControl);
如何在代码中将行添加到myGrid?
您可以使用对 它可以让你编写你的 然后从 您可以找到这些属性的详细实现在我的博客上。 I have a Grid with a fixed number of columns and unknown number of rows. The number of rows is set in the constructor once. I've tries the following line of code but it returns null: How can I add rows to "myGrid" in code? You can use attached properties for a It will allow you to write your Then just expose a property from your You can find a detailed implementation of those properties on my blog. 这篇关于我如何动态地将一个RowDefinition添加到ItemsPanelTemplate中的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Grid
修改 RowDefinitions
和 ColumnDefinitions
当这些属性被设置或改变时。
Grid
像这样:
< Grid local:GridHelpers.RowCount ={Binding MaxGridRow}
local:GridHelpers.ColumnCount = 3/>
ViewModel
它返回 Cells
集合中最大的行数。
<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>
object obj = myItemsControl.Template.FindName("myGrid", myItemsControl);
Grid
that modify the RowDefinitions
and ColumnDefinitions
when those properties are set or changed.Grid
like this:<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
local:GridHelpers.ColumnCount="3" />
ViewModel
which returns the largest row number in the Cells
collection.