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 (string,Device), где строка - это имя устройства, а устройство - это сам объект устройства.

Я понял, что если я заменю

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

с

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

Я бы получил правильный ListView для той конкретной системы, которую я хотел, но, очевидно, я хочу, чтобы это было сделано для всех систем в системной карте, а не для системы 1.

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

Отображается только до местоположения, затем системы под местоположением, но не отображаются устройства под системой.

Спасибо

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"}
  };
}
Другие вопросы по тегам