0

I've got a class structured like this:

public class WTRun : INotifyPropertyChanged
{
    public WTRun()
    {

    }

    # Local variable definitions removed
    # Property gets & sets removed
    # NotifyPropertyChanged code removed

    public Guid RunId
    public int RunNumber
    public DateTime? RunStartTime
    public DateTime? RunEndTime
    public string RunComment
    public int MapId
    public RunConfiguration Configuration
    public ObservableCollection<StepMetric> StepMetrics
    public ObservableCollection<BlockMetric> BlockMetrics
    public ObservableCollection<Medusa> Medusas
    public ObservableCollection<PIV> PIVs
    public ObservableCollection<CMotion> CMotions
}

The class is wrapped in an ObservableCollection and then bound to a TreeView. I'm getting stuck as to how to set the TreeView up so that it shows the items correctly.

I want the TreeView to look like this:

Root
  +--(RunNumber)
  |  +--"StepMetrics"
  |  |  +--(StepMetric IDs)
  |  +--"BlockMetrics"
  |  |  +--(BlockMetric IDs)
  |  +--"Medusas"
  |  |  +--(Medusa IDs)
  |  +--"PIVs"
  |  |  +--(PIV IDs)
  |  +--"CMotions"
  |     +--(CMotion IDs)
  |--(RunNumber)
     ...etc  

I've been trying with had a stab at doing it with Templates, but can only get the RunNumbers to appear so far:

<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type VMEntities:WTRun}" >
        <TextBlock Text="{Binding RunNumber }" Margin="1" />                                
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type VMEntities:BlockMetric}">
        <TextBlock Text="{Binding BlockIndex}"/>
    </HierarchicalDataTemplate>
</TreeView.Resources>

This is obviously not a complete attempt...

Whats the right way to do this?

Rob Marsh
  • 549
  • 5
  • 22
  • This makes little sense. A `TreeView` needs a hierarchical data structure. It does not understand your data model. You need to come up with a simple hierarchical data model with a single property of type `T` called Children and then model your tree after that. There's no way you'll get a bunch of arbitrary UI elements created for a lot of different properties in your class. – Federico Berasategui Jan 22 '15 at 18:00
  • That said, if (and only if) your tree is readonly (meaning the user can't change it), and it will not be dynamically modified at runtime, you'd rather just use code behind for this one. – Federico Berasategui Jan 22 '15 at 18:06
  • You can use compositecollection and build the treeview with mutiple datatypes. refer http://stackoverflow.com/questions/3673173/wpf-treeview-databinding-hierarchal-data-with-mixed-types – Ayyappan Subramanian Jan 22 '15 at 22:23
  • I've added the Composite code, but I'm getting the following error: Error 8 The type 'System.ComponentModel.ICollectionViewFactory' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. I've tried adding a reference to the assembly that contains ICollectionViewFactory,. but that doesn't seem to make any difference. – Rob Marsh Jan 23 '15 at 09:20
  • Ooops, stupid me, didn't notice the WindowsBase name lurking there... that sorted it... – Rob Marsh Jan 23 '15 at 09:22
  • OK, I can get the top level to appear with the correct value in the header, It has an expansion marker, but all the subitems for all the different types are then shown together. I need to have a further header below the main one that groups them into their respective types. I've tried various attempts at doing this with no success. – Rob Marsh Jan 23 '15 at 11:07

0 Answers0