我如何动态地将一个RowDefinition添加到ItemsPanelTemplate中的网格? [英] How can I dynamically add a RowDefinition to a Grid in an ItemsPanelTemplate?

查看:158
本文介绍了我如何动态地将一个RowDefinition添加到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?

解决方案

您可以使用 Grid 修改 RowDefinitions ColumnDefinitions 当这些属性被设置或改变时。

它可以让你编写你的 Grid 像这样:

 < Grid local:GridHelpers.RowCount ={Binding MaxGridRow}
local:GridHelpers.ColumnCount = 3/>

然后从 ViewModel 它返回 Cells 集合中最大的行数。



您可以找到这些属性的详细实现在我的博客上。


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.

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

I've tries the following line of code but it returns null:

object obj = myItemsControl.Template.FindName("myGrid", myItemsControl);

How can I add rows to "myGrid" in code?

解决方案

You can use attached properties for a Grid that modify the RowDefinitions and ColumnDefinitions when those properties are set or changed.

It will allow you to write your Grid like this:

<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
      local:GridHelpers.ColumnCount="3" />

Then just expose a property from your ViewModel which returns the largest row number in the Cells collection.

You can find a detailed implementation of those properties on my blog.

这篇关于我如何动态地将一个RowDefinition添加到ItemsPanelTemplate中的网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆