是否可以为“普通”集合定义TreeView分层视图模板? [英] Is it possible to define a TreeView hierarchical view template for a 'plain' collection?
问题描述
我的模型类如下:
class Base
{
public string Name { get; set; }
}
class Item : Base { ... }
class Group : Base
{
public List<Base> GroupItems { get; private set; }
}
然后,我有一个的视图模型我填写的项目
集合,如下面的 FillUp()
方法所示:
Then, I have a view model with an Items
collection, that I fill up like shown in the FillUp()
method below:
class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<Base> Items { get; set; }
private void FillUp()
{
Item item1 = new Item { Name = "Item1" };
Item item2 = new Item { Name = "Item2" };
Group group = new Group { Name = "Group" };
group.GroupItems.Add(item1);
this.Items.Add(item1);
this.Items.Add(item2);
this.Items.Add(group);
}
}
因此,我的项目
集合现在包含2个类型为 Item
的对象和一个类型为 Group
的对象对 Item
对象之一的父母-孩子引用。
So, my Items
collection now contains 2 objects of type Item
and one object of type Group
, which has a "parent-child" reference to one of the Item
object.
我想要实现的目标:希望在 TreeView
中显示此模型,以便所有 Item
都属于某些尽管组
应该显示为该组
的子元素,尽管它们位于根集合中。
What I want to achieve: I'd like to display this model in a TreeView
so that all the Item
s which belong to some Group
should be displayed as child elements of that Group
, despite of they are in the "root" collection.
它应该看起来像这样:
- Item2
+ Group
- Item1
我可以使用 HierarchicalDataTemplate
来显示父子元素,但这不能让我从根集合中过滤掉属于某些组的那些项。
I could use a HierarchicalDataTemplate
to display parent-child elements, but this does not let me filtering out those items from the root collection that belong to some groups.
<TreeView>
<TreeView.Resources>
<HierarchicalDataTemplate DataType = "{x:Type loc:Group}" ItemsSource = "{Binding GroupItems}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type loc:Item}">
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</TreeView.Resources>
<TreeViewItem ItemsSource="{Binding Items}"/>
</TreeView>
使用此视图,我得到了 Item1
两次:
With this view, I'm getting the "Item1"
twice:
- Item1
- Item2
+ Group
- Item1
是否有任何方法只能通过视图(XAML)标记获得所需的结果?否则,我应该更改视图模型,以便将 Item
s仅添加到 Group
s中,而不添加到
Is there any way to get the desired result via view (XAML) markup only? Otherwise, I should change my view model so that the Item
s will be only added into the Group
s, not in the "root" collection (but that is what I try to avoid at the moment).
推荐答案
假设您有以下迹象:哪个项目属于一个组,在此示例中,我在组和项目类中添加了Guid。
Assuming you have some indication of which Item belongs in a group , for this example iv'e added a Guid in Group and Item classes.
您可以通过以下两种方式进行操作:
You can go about this in 2 ways :
1)创建一个复合包装器类。
并创建Wrapper类的ItemsSource。如果存在单个不属于某个组的项目,则子代将保持为空。
1) Create a composite wrapper class . And create an ItemsSource of Wrapper class . Where there is a single item which does not belong to a group , Children would just remain null.
public class Wrapper<T> where T : Base
{
public List<T> Children { get; set; }
}
public class Base
{
public string Name { get; set; }
}
public class Item : Base
{
public Guid GroupId { get; set; }
}
public class Group : Base
{
public Guid ID { get; set; }
public List<Item> GroupItems { get; private set; }
}
第二个选择是使用CollectionView,并过滤出属于
your second option is to use a CollectionView , and filter out Items which belong to a group.
private ICollectionView _baseView;
public ICollectionView BaseView
{
get
{
if (_baseView == null)
{
_baseView = new ListCollectionView((IList) items);
_baseView.Filter = o => o is Group || o is Item && (o as Item).GroupId != Guid.Empty;
}
return _baseView;
}
}
这篇关于是否可以为“普通”集合定义TreeView分层视图模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!