5

我刚刚开始学习 WPF,我正在尝试在 ItemsControl 中使用 GridViewRowPresenter 来基本上复制 HTML 中简单表格的功能。ListView 不合适,因为它是交互式的(我不想要)。我绑定到未知数量的对象的通用列表。

我有一个自定义对象的列表,该对象具有两个字符串属性:名字和姓氏。以下代码有效:

<ItemsControl Name="myItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=FirstName}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

虽然这没有任何效果:

<ItemsControl Name="myItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <GridViewRowPresenter>
                <GridViewRowPresenter.Columns>
                    <GridViewColumnCollection>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}"></GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Path=LastName}"></GridViewColumn>
                    </GridViewColumnCollection>
                </GridViewRowPresenter.Columns>
            </GridViewRowPresenter>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我不知道从这里去哪里,我将不胜感激任何帮助!谢谢!

4

1 回答 1

11

如果您想要一个非交互式的项目网格,您可以使用一个使用共享大小范围的ItemsControlwith a Grid

<ItemsControl ItemsSource="{Binding Items}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" SharedSizeGroup="FirstName"/>
                    <ColumnDefinition Width="*" SharedSizeGroup="LastName"/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Grid.Column="1" Text="{Binding LastName}"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

一种更有效的方法是编写您自己的子类,其工作方式与(您可能可以子类)Panel类似,但会根据需要自动添加行。然后将其用作.GridGridPanelItemsPanelItemsControl

于 2009-03-27T20:47:22.857 回答