TreeView分组

时间:2014-01-15 20:35:09

标签: c# wpf mvvm treeview

我的目标是从单个集合创建树视图,但是根据特定属性对其进行分组,如下所示:

(Alphabetical View)
      Test Machine 1
    > Test Machine 2
        |_ Test Device 1
        |_ Test Device 2
    > Test Machine 3
        |_ Test Device 3
        |_ Test Device 4
      Test Machine 4

(Location View)
    > Chicago
        |_ Test Machine 1
        |_ Test Machine 4       
    > Green Bay
        |_ Test Machine 3
        |_ Test Device 3
        |_ Test Device 4
    > Louisville
        |_ Test Machine 2
            |_ Test Device 1
        |_ Test Device 2

(Status View)
    > Connected
        |_ Test Machine 1
        |_ Test Machine 2
            |_ Test Device 1
            |_ Test Device 2
    > Disconnected
        |_ Test Machine 3
            |_ Test Device 3
            |_ Test Device 4

    > Unknown
        |_ Test Machine 4

(Device Type View)
    > Type A
        |_ Test Device 1
        |_ Test Device 3
    > Type B        
        |_ Test Device 2
        |_ Test Device 4

以下是我的课程的基本细分和上述示例的一些示例数据:

public class Machine
{
    public string MachineName { get; set;}
    public string Location { get; set;} 
    public IList<Device> Devices { get; set;}
    public Status Status { get; set;}
}

public class Device
{
    public string DeviceName { get; set;}
    public string DeviceType { get; set;}
}

public enum Status
{
    Unknown,
    Disconnected,
    Connected
}

public IList<Machine> Machines
{
    get 
    {
        return new IList<Machine>()
        {
            new Machine { MachineName = "Test Machine 1", Location = "Chicago", Status = Status.Connected, Devices = new List<Device>() },
            new Machine { MachineName = "Test Machine 2", Location = "Louisville", Status = Status.Connected, Devices = new List<Device>
                                                                                                                            {
                                                                                                                                new Device { DeviceName = "Test Device 1", DeviceType = "Type A"},
                                                                                                                                new Device { DeviceName = "Test Device 2", DeviceType = "Type B"},
                                                                                                                            }},
            new Machine { MachineName = "Test Machine 3", Location = "Green Bay", Status = Status.Disconnected, Devices = new List<Device>
                                                                                                                            {
                                                                                                                                new Device { DeviceName = "Test Device 3", DeviceType = "Type A"},
                                                                                                                                new Device { DeviceName = "Test Device 4", DeviceType = "Type B"},
                                                                                                                            } },
            new Machine { MachineName = "Test Machine 4", Location = "Chicago", Status = Status.Unknown, Devices = new List<Device>() }
        }
    }
}

目前,我通过在主ViewModel中为TreeView的每个分组创建子ViewModel来实现此目的。然后,按下按钮,我将ViewModel更改为ContentControl。然后,ContentControl能够使用隐式模板专门为该分组绘制它。

有没有办法使用CollectionViewSource执行此操作,并且它的列出功能如here所列?我想这可以节省我7个班级......

1 个答案:

答案 0 :(得分:1)

如果您的商品数量会很大,我打赌您当前的解决方案最适合,因为您可以应用线程。

CollectionViewSourceGrouping将具有非线性GUI线程,并且还会松散Virtualization ThreeView

作为一名开发人员,我宁愿不减少课程数量,即使你也是如此。 : - )