Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I click on the button system throws exception
System.InvalidOperationException: 'Items collection must be empty before using ItemsSource.'

I am unable to find the problem

What I have tried:

I am trying to create a tree when click on a button like this
C#
<pre>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 processes = new List<RunProcesses>
        {
            new RunProcesses{ Name = "Process1", ChildNodes = nodes }
        };
            TreeView.ItemsSource = processes;

        }
Posted
Updated 10-Jun-18 2:36am

1 solution

Read the error message, it's pretty explicit:
System.InvalidOperationException: 'Items collection must be empty before using ItemsSource.'
You are setting the ItemsSource property of a collection (the Treeview) which already has data in it.

Check your XAML, or empty the Items Collection.
 
Share this answer
 
Comments
Member 13688663 10-Jun-18 8:59am    
<TreeView x:Name="TreeView">
        <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})}" />
                
            
            <HierarchicalDataTemplate DataType="{x:Type local:Module}" ItemsSource="{Binding ChildNodes}">
                <TextBlock Text="{Binding Path=Name}" />
            

            <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})}" />
                
            
            <HierarchicalDataTemplate DataType="{x:Type local:SubThread}" ItemsSource="{Binding ChildNodes}">
                <TextBlock Text="{Binding Path=Name}" />





This is xmal code and itemsource is empty
Member 13688663 10-Jun-18 9:00am    
These are classes
public interface ITreeNode
{
string Name { get; set; }
List<itreenode> ChildNodes { get; set; }
}
public class RunProcesses : ITreeNode
{
public string Name { get; set; }
public int ID { get; set; }
public List<itreenode> ChildNodes { get; set; }
}
public class Module : ITreeNode
{
public string Name { get; set; }
public List<itreenode> ChildNodes { get; set; }
}
public class Thread : ITreeNode
{
public string Name { get; set; }
public int ID { get; set; }
public List<itreenode> ChildNodes { get; set; }
}
public class SubModule : ITreeNode
{
public string Name { get; set; }
public List<itreenode> ChildNodes { get => null; set => throw new System.NotImplementedException(); }
}
public class SubThread : ITreeNode
{
public string Name { get; set; }
public List<itreenode> ChildNodes { get => null; set => throw new System.NotImplementedException(); }
}
Member 13688663 10-Jun-18 9:02am    
Kindly guide me where I am wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900