0

I want to use the HierarchicalDataTemplate with a TreeView control in WPF. I created a viewmodel class but a System.StackOverflowException occurs every time the program starts. I have no idea why this happens.

Here is the WPF markup:

    <TreeView Grid.Row="2" ItemsSource="{Binding ImportTasks}">

        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type VM:SyncToolViewModel}" ItemsSource="{Binding Path=ImportTasks}">
                <Label Content="{Binding }"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type VM:VmImportTask}" ItemsSource="{Binding Path=ImportTasks.Tables}">
                <Label Content="{Binding }"/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>

and here is the code part:

public class SyncToolViewModel
{
    public DBSyncToolLib.DataContext DataContext { get; set; }

    private List<VmImportTask> _ImportTasks;
    public List<VmImportTask> ImportTasks
    {
        get
        {
            if (_ImportTasks == null)
            {
                _ImportTasks = (from IT in DataContext.ImportTask
                                select
                                new VmImportTask()
                                {
                                   DBImportTask = IT
                                }
                               ).ToList();
            }
            return _ImportTasks;
        }
    }
}

public class VmImportTask
{

    public DBSyncToolLib.Schema.ImportTask DBImportTask { get; set; }

    public List<VmImportTaskTable> Tables
    {
        get
        {
            var L = (from B in DBImportTask.ImportTaskTable
                    select new VmImportTaskTable()
                    {
                        DBImportTaskTable = B
                    }).ToList();
            return L;
        }
    }

    public override string ToString()
    {
        return this.DBImportTask.Name;
    }
}
Gerrit Horeis
  • 541
  • 5
  • 14

2 Answers2

0

I am not sure, but so

<HierarchicalDataTemplate DataType="{x:Type VM:VmImportTask}" ItemsSource="{Binding Path=Tables}">

or so

<HierarchicalDataTemplate DataType="{x:Type VM:SyncToolViewModel}" ItemsSource="{Binding Path=ImportTasks.Tables}">
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

instead of HierarchicalDataTemplate you can also use simply styles.

a little example:

public partial class MainWindow : Window
{
    public List<Parent> Parents { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        this.Parents = new List<Parent>();
        this.Parents.Add(new Parent(){ParentName = "Parent1",Childs = new ObservableCollection<Child>(){new Child(){ChildName = "Child1"},new Child(){ChildName = "Child2"}}});
        this.Parents.Add(new Parent() { ParentName = "Parent2", Childs = new ObservableCollection<Child>() { new Child() { ChildName = "Child3" }, new Child() { ChildName = "Child4" } } });

        this.DataContext = this.Parents;
    }
}

public class Parent
{
    public string ParentName { get; set; }
    public ObservableCollection<Child> Childs { get; set; }
}

public class Child
{
    public string ChildName { get; set; }
}

xaml

    <TreeView ItemsSource="{Binding}">
        <!-- First Level-->
        <TreeView.ItemContainerStyle>
            <Style>
                <Setter Property="TreeViewItem.Header" Value="{Binding ParentName}"/>
                <Setter Property="TreeViewItem.ItemsSource" Value="{Binding Childs}"/>
                <!-- Second Level -->
                <Setter Property="TreeViewItem.ItemContainerStyle">
                    <Setter.Value>
                        <Style>
                            <Setter Property="TreeViewItem.Header" Value="{Binding ChildName}"/>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView> 
blindmeis
  • 22,175
  • 7
  • 55
  • 74