问题描述
我刚刚开始学习WPF,我试图在ItemsControl里面使用一个GridViewRowPresenter来基本上复制HTML中的简单表的功能。 ListView是不合适的,因为它是交互式的(我不想要的)。我绑定到一个未知数量的对象的通用列表。我有一个具有两个字符串属性的自定义对象的列表:FirstName和LastName。以下代码工作:
< 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>
我不知道从哪里去,我非常感谢任何帮助!谢谢!
如果您想要一个非互动的项目网格,可以使用 ItemsControl
使用共享大小范围的网格
< 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 =1Text ={Binding LastName}/>
< / Grid>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
更有效的方法是编写自己的面板
子类,类似于 Grid
(你可能可以子类 Grid
),但是根据需要自动添加行。然后使用面板
作为 ItemsPanel
为 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. The ListView is not appropriate since it is interactive (which I don't want). 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. 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 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. Then use that Panel
as the ItemsPanel
for the ItemsControl
.
这篇关于一个ItemsControl中的WPF GridViewRowPresenter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!