Search code examples
c#winformsobjectlistview

ObjectListView Nested Objects


I am currently trying to use TreeListView, and I was wondering how you would find values of three layered nested object, where the layers are location, then system, then device.

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

There is a concurrent dictionary systemMap (string,Device) where the string is the name of the device, and the device is a device object itself.

I got it to a point where if i replace

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

with

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

I would get the correct ListView for that specific system I wanted, but obviously I want it done to all systems in the system map, instead of system 1.

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

only shows up to location, then systems under the location, but does not show devices under system.

Thank you


Solution

  • Sample run: sample run

    Model classes:

    // 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 { }
    

    Form constructor:

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