0

I am beginner in c# and this is my first program in which I am trying to add data in tree and display it when click on a button
This is the code of button that add data in treeview and display it

   public MainWindow()
    {
        InitializeComponent();
    }
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var subModules = new List<ITreeNode>
    {
        new SubModule { Name = "Sub Module 1" }
    };
        var subThreads = new List<ITreeNode>
    {
        new SubThread { Name = "Sub Thread 1" }
    };
        var nodes = new List<ITreeNode>
    {
        new Thread { Name = "Thread ", ChildNodes = subThreads },
        new Module { Name = "Module ", ChildNodes = subModules }
    };
        var runprocesses = new List<RunProcesses>
    {
        new RunProcesses{ Name = "Process1", ChildNodes = nodes }
    };
         TreeView.ItemsSource = RunProcesses;


    }

This is xmal code

<TreeView x:Name="TreeView">
   ` <Button Content="Button" VerticalAlignment="Top" 
     Width="75"Click="Button_Click_1"/>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:RunProcesses}" 
        ItemsSource="{Binding ChildNodes}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name, StringFormat='{}{0} '}"/>
                <TextBlock Text="{Binding Path=ID, StringFormat=(ID: {0})}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Module}" ItemsSource="{Binding ChildNodes}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Thread}" ItemsSource="{Binding ChildNodes}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Name, StringFormat='{}{0} '}" />
                <TextBlock Text="{Binding Path=ID, StringFormat=(ID: {0})}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:SubThread}" ItemsSource="{Binding ChildNodes}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>

</TreeView>

This code compile and run properly but when I click on button system throws an exception

System.invalidoperationexception: 'items collection must be empty before using itemssource.'

I put much efforts in identifying reason of exception but unsuccessful
Kindly guide me where I am wrong

  • See if [this thread](https://stackoverflow.com/questions/683863/items-collection-must-be-empty-before-using-itemssource) helps. – CodingYoshi Jun 10 '18 at 15:42
  • I already check this link –  Jun 10 '18 at 16:02
  • In that link everyone add an additional tag to resolve problem but I don't know which tag I need :( –  Jun 10 '18 at 16:08
  • One thing I notice is this: `TreeView.ItemsSource = RunProcesses;`. I am pretty sure it should be `TreeView.ItemsSource = runprocesses;` – CodingYoshi Jun 10 '18 at 16:14
  • Thanks for pointing it out but after changing it system again throws same exception –  Jun 10 '18 at 16:34

1 Answers1

0

You need to move the <button> markup outside the TreeView. Otherwise, TreeView ItemCollection property is initialized such that ItemCollection has a Button element and hence you can't set the ItemsSource as described by the error.

Consider arranging your treeview and button in some container instead of nesting button inside treeview :

<StackPanel>
  <Button/>
  <TreeView/>
</StackPanel>
Nish26
  • 969
  • 8
  • 16