简体   繁体   English

ItemsControl中的WPF GridViewRowPresenter

[英]WPF GridViewRowPresenter in an ItemsControl

I'm just starting learning WPF and I'm trying to use a GridViewRowPresenter inside of an ItemsControl to essentially duplicate the functionality of a simple table in HTML. 我刚刚开始学习WPF,我试图在ItemsControl中使用GridViewRowPresenter来实质上复制HTML中的简单表的功能。 The ListView is not appropriate since it is interactive (which I don't want). ListView不合适,因为它是交互式的(我不想要)。 I am binding to a generic List of objects of an unknown quantity. 我绑定到未知数量的对象的通用列表。

I have a List of a custom object that has two string properties: FirstName and LastName. 我有一个自定义对象的列表,它有两个字符串属性:FirstName和LastName。 The following code works: 以下代码有效:

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

while this renders nothing: 虽然这没有任何结果:

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

I'm not sure where to go from here and I would greatly appreciate any help! 我不知道从哪里开始,我非常感谢任何帮助! Thanks! 谢谢!

If you want a non-interactive grid of items, you can use an ItemsControl with a Grid that uses shared size scope: 如果需要非交互式项目网格,可以将ItemsControl与使用共享大小范围的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>

A more efficient approach would be to write your own Panel subclass that works similarly to Grid (you could probably subclass Grid ) but automatically adds rows as necessary. 一种更有效的方法是编写自己的Panel子类,其工作方式与Grid类似(您可能可以继承Grid ),但会根据需要自动添加行。 Then use that Panel as the ItemsPanel for the ItemsControl . 然后使用该PanelItemsPanelItemsControl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM