frankieee
frankieee

Reputation: 13

Display treeviewitem as grid rows in wpf where the branches might have a different depth

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?

Upvotes: 1

Views: 1133

Answers (1)

Related Questions