1

I am trying to create a TreeView with a number of column data in each row (e.g. 2 columns in my example, property and value), similar to this thread. The difference to the solution proposed by Heena is that the tree might be a little more complex. The depth for each branch might be different (so in some cases, nodes could have more children than others).

I would realize the code with the following class, such that nested elements are possible:

public class TreeItem
{
  private TreeItem()
  {
  }

  public TreeItem( string property, string value )
  {
    if( property == null )
    {
      property = string.Empty;
    }

    if( value == null )
    {
      value = string.Empty;
    }

    _property = property;
    _value = value;
  }

  public TreeItem( params TreeItem[] items )
  {
    AddItems( items );
  }

  public TreeItem( string property, string value, params TreeItem[] items ) : this( property, value )
  {
    AddItems( items );
  }

  public string Property
  {
    get
    {
      return _property;
    }
  }

  public string Value
  {
    get
    {
      return _value;
    }
  }

  public IEnumerable<TreeItem> Items
  {
    get
    {
      return (IEnumerable<TreeItem>)_items;
    }
  }

  private void AddItems( params TreeItem[] items )
  {
    foreach( TreeItem item in items )
    {
      if( item != null )
      {
        _items.Add( item );
      }
    }
  }

  private readonly string _property;

  private readonly string _value;

  private readonly List<TreeItem> _items = new List<TreeItem>();
}

Is it possible to realize this with WPF? If yes, how?

Victor
  • 868
  • 8
  • 22
frankieee
  • 13
  • 4
  • Instead of creating the tree by hand, it might actually be easier to modify the datatemplate to adhere to the data and bind to it. How is your data structured? Can you give an example of the data instead? – ΩmegaMan Jul 12 '15 at 01:35
  • There is a ASN1 based communication protocol. The classes are automatically created using an ASN1 compiler. From those classes, I plan to implement child classes as a ViewModel, to get a TreeItem out of their own properties. When data comes in from TCP/IP, a decoder translate the data into these ASN1 objects automatically, so the tree is generated automatically in the end. – frankieee Jul 16 '15 at 09:08