【问题标题】:ObjectListView Nested ObjectsObjectListView 嵌套对象
【发布时间】:2015-07-28 17:45:28
【问题描述】:

我目前正在尝试使用 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(string,Device),其中字符串是设备的名称,设备本身就是一个设备对象。

我已经达到了如果我更换的地步

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

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

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

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

只显示到位置,然后显示该位置下的系统,但不显示系统下的设备。

谢谢

【问题讨论】:

    标签: c# winforms objectlistview


    【解决方案1】:

    示例运行:

    模型类:

    // 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"}
      };
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 2014-05-06
      • 2017-10-03
      • 2021-12-23
      • 2022-01-08
      • 2020-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多