0

I want to create something like a TreeView with the ListView Control. There are two classes with the same base-class like this:

public abstract class DataObjectBase
{
    public string Name { get; set; }

    public override string ToString()
    {
        return this.Name;
    }
}

public class SimpleDataObject : DataObjectBase
{
    public object Value { get; set; }
}

public class ComplexDataObject : DataObjectBase
{
    private ObservableCollection<DataObjectBase> _DataObjects = new ObservableCollection<DataObjectBase>();

    public ObservableCollection<DataObjectBase> DataObjects
    {
        get { return _DataObjects; }
        set { _DataObjects = value; }
    }
}

How can I bind this structure to a ListView using a HirarchicalDataTemplate? The complex DataObject can have n subelements in the _DataObjects ObservableCollection.

Michi-2142
  • 1,170
  • 3
  • 18
  • 39

1 Answers1

4

The answer is ... you don't. If ou look at the documentation http://msdn.microsoft.com/en-us/library/system.windows.hierarchicaldatatemplate(v=vs.110).aspx HierarchicalDataTemplates can only be used with HeaderedItemsControls. The ListView inherits from simple ItemsControl.

You can either use a TreeView or use classic DataTemplates to implement it.

Dmitry
  • 2,033
  • 1
  • 22
  • 31
  • Ok thanks for the fast answer. Do you have an example for the classic way? Otherwise I have to use the TreView. – Michi-2142 Apr 15 '14 at 16:30
  • Oups I was wrong :( It's not possible to define a template for the ListView row like for the ListBox. – Dmitry Apr 15 '14 at 17:01