【问题标题】:Is it possible to use an existing grid when creating an ItemsControl?创建 ItemsControl 时是否可以使用现有网格?
【发布时间】:2013-02-20 10:24:28
【问题描述】:

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

我希望将所有控件托管在一个网格中,以便我可以从上到下、从左到右进行制表操作。我正在尝试使用的代码如下所示:

<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 属性设置为封闭网格,但不幸的是这不起作用。

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

还有其他方法吗?还是我必须自己推出ItemsControl

【问题讨论】:

    标签: c# .net wpf itemscontrol


    【解决方案1】:

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

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

    【讨论】:

    • 能否请您提供一个指向 MSDN 的链接?
    • @IronMan84 检查更新的文本 - 我在“MSDN”字后面放了一个链接。
    • 您可以使用一些类似网格的控件,例如 ListView(View=GridView)或 DataGrid。我认为后者更有可能成为您任务的可行选择。
    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    相关资源
    最近更新 更多