对复合集合进行排序

对复合集合进行排序

本文介绍了对复合集合进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以WPF不支持CompositeCollections视图的标准排序或过滤行为,所以解决这个问题的最佳实践是什么。

So WPF doesn't support standard sorting or filtering behavior for views of CompositeCollections, so what would be a best practice for solving this problem.

有两个或更多不同类型的对象集合。您想要将它们合并为一个可排序和可过滤的集合(必须手动实现排序或过滤)。

There are two or more object collections of different types. You want to combine them into a single sortable and filterable collection (withing having to manually implement sort or filter).

我考虑的一种方法是创建一个新对象集合只有一些核心属性,包括我想要的集合排序,以及每个类型的对象实例。

One of the approaches I've considered is to create a new object collection with only a few core properties, including the ones that I would want the collection sorted on, and an object instance of each type.

class MyCompositeObject
{
    enum           ObjectType;
    DateTime       CreatedDate;
    string         SomeAttribute;
    myObjectType1  Obj1;
    myObjectType2  Obj2;
{
class MyCompositeObjects : List<MyCompositeObject> { }

然后循环遍历我的两个对象集合,构建新的复合集合。显然这是一个强力的方法,但它会工作。我会得到我的新的复合对象集合的所有默认视图排序和过滤行为,我可以在它上面放置一个数据模板,以正确显示我的列表项,根据实际存储在该复合项中的类型。

And then loop through my two object collections to build the new composite collection. Obviously this is a bit of a brute force method, but it would work. I'd get all the default view sorting and filtering behavior on my new composite object collection, and I'd be able to put a data template on it to display my list items properly depending on which type is actually stored in that composite item.

有什么建议以更优雅的方式做?

What suggestions are there for doing this in a more elegant way?

推荐答案

更新:我发现了一个更优雅的解决方案:

Update: I found a much more elegant solution:

class MyCompositeObject
{
    DateTime    CreatedDate;
    string      SomeAttribute;
    Object      Obj1;
{
class MyCompositeObjects : List<MyCompositeObject> { }

我发现由于反射,存储在Obj1中的特定类型在运行时解析,类型特定的DataTemplate按预期应用!

I found that due to reflection, the specific type stored in Obj1 is resolved at runtime and the type specific DataTemplate is applied as expected!

这篇关于对复合集合进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:15