0

I can't get my data to appear in a tree-view. I have a label above the treeview that is bound to my viewmodel and that works. What have I done wrong with the tree-view?

Here's what I've done.

Model:

public class ViewConfiguration
{
    public string Name { get; set; }
    public List<ViewRuleSet> RuleSets { get; set; }

    public ViewConfiguration(string name, params ViewRuleSet[] ruleSets)
    {
        this.Name = name;
        this.RuleSets = new List<ViewRuleSet>(ruleSets);
    }
}

public class ViewRuleSet
{
    public string Name { get; set; }
    public List<ViewRule> Rules { get; set; }

    public ViewRuleSet(string name, params ViewRule[] rules)
    {
        this.Name = name;
        this.Rules = new List<ViewRule>(rules);
    }
}

public class ViewRule
{
    public string Name { get; set; }

    public ViewRule(string name)
    {
        this.Name = name;
    }
}

ViewModel:

public class MainViewModel
{
    public ViewConfiguration Configuration { get; set; }

    public MainViewModel()
    {
        var rule = new ViewRule("MyRule");

        var ruleSet = new ViewRuleSet("MyRuleSet", rule);

        this.Configuration = new ViewConfiguration("MyConfiguration", ruleSet);
    }
}

View:

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:TreeViewer.ViewModel" 
    xmlns:m="clr-namespace:TreeViewer.Model"
    x:Class="TreeViewer.MainWindow"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <vm:MainViewModel/>
</Window.DataContext>

<Grid>
    <Label Margin="10,10,10,0" VerticalAlignment="Top" Content="{Binding Configuration.Name}"/>
    <TreeView ItemsSource="{Binding Configuration}" Margin="0,41,0,0">

        <!-- Configuration template -->
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding RuleSets}">
                <TextBlock Foreground="Red" Text="{Binding Name}" />

                <!-- RuleSet template -->
                <HierarchicalDataTemplate.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Rules}">
                        <TextBlock Text="{Binding Name}" />

                        <!-- Rule template -->
                        <HierarchicalDataTemplate.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name}" />
                            </DataTemplate>
                        </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>

            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>

    </TreeView>
</Grid>

Here's what I see: (I've also tried explicitly setting the DataContext of my main window through code. this.DataContext = new MainViewModel();)

Treeview with no data

Askolein
  • 3,250
  • 3
  • 28
  • 40
PeteGO
  • 5,597
  • 3
  • 39
  • 70
  • 2
    The value of `TreeView.ItemsSource` must be a collection, not a single object. You should bind to `Configuration.RuleSets` instead of `Configuration`. Of course, you must adapt your `HierarchicalDataTemplate`s accordingly. –  Jun 28 '14 at 14:40
  • Thanks - I just changed the Configuration in the ViewModel to be `List Configurations` and it worked. – PeteGO Jun 28 '14 at 14:50
  • you could of just have add him implement IList and returned implemented it to accommodate Rules – eran otzap Jun 29 '14 at 01:34
  • _"The value of TreeView.ItemsSource must be a collection"_ <---- YEP took me 3 hours to find this message.... The writer of the code, which is not giving an exception, should be fired! Thanks for pointing this out. – Jeroen van Langen Mar 20 '22 at 22:20

0 Answers0