一个ItemsControl中的WPF GridViewRowPresenter

编程入门 行业动态 更新时间:2024-10-28 19:20:11
本文介绍了一个ItemsControl中的WPF GridViewRowPresenter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我刚刚开始学习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

本文发布于:2023-11-12 03:05:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ItemsControl   WPF   GridViewRowPresenter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!