Biz*_*han 12 wpf grid templates itemscontrol
我正在尝试创建一个具有可变行数和列数的表.我有这样做ItemsControl具有Grid作为ItemsPanel.我知道我可以设置Grid.Row和Grid.Column每个项目通过的ItemContainerStyle.但是当我无法通过名称访问Grid时,我不知道如何更改行数和列数以及它们的大小.
你如何修改RowDefinitions或ColumnDefinitions的Grid使用只在运行时XAML和结合与无代码隐藏?
这是XAML代码:
<ItemsControl Name="myItemsControl" ItemsSource="{Binding Cells}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid Name="myGrid">
                <Grid.RowDefinitions>
                    <!-- unknown number of rows are added here in run-time -->
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <!-- known number of columns are added here in run-time -->
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style.../>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>
我试图添加一些RowDefinition代码,但我找不到myGrid通过其名称(或任何其他方式)访问的方法,因为它在一个内部ItemsPanelTemplate.
我想知道是否有任何方法可以在运行时以编程方式添加或修改RowDefinitions?
Rac*_*hel 28
您可以使用附加属性来Grid修改,RowDefinitions以及ColumnDefinitions何时设置或更改这些属性.
它可以让你这样写Grid:
<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
      local:GridHelpers.ColumnCount="3" />
然后只显示一个属性ViewModel,返回Cells集合中最大的行号.
您可以在我的博客上找到这些属性的详细实现.
Y.Y*_*hus 10
如果您可以从代码隐藏中访问网格,则可以执行以下操作:
var rowDefinition = new RowDefinition();
rowDefinition.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowDefinition);