1

我正在尝试创建一个布局,其中行数和列数都是动态的,如下所示:

图片

我希望将所有控件都托管在一个网格中,这样我就可以从上到下、从左到右进行制表操作。我试图开始工作的代码如下所示:

<Grid base:GridHelpers.RowCount="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=dx:DXWindow}, Path=Groups.Count}" Name="EnclosingGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="180" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <!--Labels-->
    <ItemsControl ItemsSource="{Binding Rows}" ItemsPanel="{Binding ElementName=EnclosingGrid}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Row" Value="{Binding RowIndex}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Label Grid.Column="0" Content="{Binding Path=FieldName}" VerticalContentAlignment="Center" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    <!--Process fields-->
    <ItemsControl ItemsSource="{Binding Cells}" ItemsPanel="{Binding ElementName=EnclosingGrid}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Row" Value="{Binding ParentRow.RowIndex}" />
                <Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

我尝试将ItemsPanel属性设置为封闭网格,但不幸的是这不起作用。

真的是将网格设置为ItemsPanelTemplate内部的唯一方法ItemsControl吗?

还有其他方法吗?还是我必须自己动手ItemsControl

4

1 回答 1

0

ItemsPanel属性类型是ItemsPanelTemplate,因此您不能将此属性直接绑定到某些现有控件。

您可以将此属性视为一种工厂类,它应该为项目提供容器控制。这就是MSDN对这种类型的字面意思:指定ItemsPresenter为 ItemsControl 的项目布局创建的面板。

于 2013-02-20T14:50:07.137 回答