c# – ObjectListView嵌套对象

我目前正在尝试使用TreeListView,我想知道如何找到三层嵌套对象的值,其中图层是位置,然后是系统,然后是设备.

private void initObjectListView()
    {
        // Can the given object be expanded?
        this.treeListView1.CanExpandGetter = delegate(Object x)
        {
            return x is Location;
        };

        // What objects should belong underneath the given model object?
        this.treeListView1.ChildrenGetter = delegate(Object x)
        {
            if (x is Location)
                return ((Location)x).systemMap.Values;


            throw new ArgumentException("Unknown TreeListView Object - Should a Location Type");
        };
    }

有一个并发字典systemMap(字符串,设备),其中字符串是设备的名称,设备本身就是设备对象.

我把它弄到了如果我更换的地步

return ((Location)x).systemMap.Values;

return ((Location)x).systemMap["System 1"].deviceMap.Values;

我会为我想要的特定系统获得正确的ListView,但显然我希望它完成系统映射中的所有系统,而不是系统1.

return ((Location)x).systemMap.Values; 

仅显示位置,然后显示位置下的系统,但不显示系统下的设备.

谢谢

最佳答案 样品运行:
《c# – ObjectListView嵌套对象》

型号类:

// column aspect names are the names of these properties
public class AspectBindable
{
  public string ObjectName { get; set; }
  public string ObjectType { get; set; }
}
// level 2 info
public class Sistem : AspectBindable { public IList <Device> Devices { get; set; } }

//level 1
public class Location : AspectBindable { public IList<Sistem> Systems { get; set; } }

//level 3 
public class Device : AspectBindable { }

表单构造函数:

public Form31683555 ( )
{
  InitializeComponent();
  // control expansion
  this.tlv.CanExpandGetter = delegate(object x)
  {
    if (x is Sistem)
      return !ObjectListView.IsEnumerableEmpty((x as Sistem).Devices);
    else if (x is Location)
      return !ObjectListView.IsEnumerableEmpty((x as Location).Systems);
    else if (x is Device)
      return false;
    else
      throw new ArgumentException("x");
  };
  // node children
  this.tlv.ChildrenGetter = delegate(object x) {
    if (x is Sistem)
      return (x as Sistem).Devices;
    else if (x is Location)
      return (x as Location).Systems;
    else if (x is Device)
      return null;
    else
      throw new ArgumentException("x");
  };
  // TreeListView first order parent level
  this.tlv.Roots = new Location[] { 
    new Location { 
      Systems = new Sistem[] { 
        new Sistem { 
          Devices = new Device[] { 
            new Device { ObjectName = "Device 1.1.1", ObjectType="some device"},
            new Device { ObjectName = "Device 1.1.2", ObjectType="a device"}
          },
          ObjectName = "System 1.1", ObjectType = "system"
        },
        new Sistem { 
          Devices = new Device[] { 
            new Device { ObjectName = "Device 1.2.1", ObjectType="device"},
            new Device { ObjectName = "Device 1.2.2", ObjectType="another device"}
          },
          ObjectName = "System 1.2", ObjectType = "another system"
        },            
      },
      ObjectType = "location 1", ObjectName="full location"
    },
    new Location {ObjectName = "empty", ObjectType="location"}
  };
}
点赞