Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在使用ItemsSource之前,Items集合必须为空。特雷维电视台_C#_Wpf_Xaml_Telerik_Treeview - Fatal编程技术网

C# 在使用ItemsSource之前,Items集合必须为空。特雷维电视台

C# 在使用ItemsSource之前,Items集合必须为空。特雷维电视台,c#,wpf,xaml,telerik,treeview,C#,Wpf,Xaml,Telerik,Treeview,我在尝试giva my treeview查看itemsource时遇到此错误 在使用ItemsSource之前,Items集合必须为空 我检查了很多解决方案,但我似乎找不到解决这个问题的方法。以下是我的代码片段: XAML: C-我的数据库查询: public List<Category> Get_Categories() { using (var context = new ProcessDatabaseEntities())

我在尝试giva my treeview查看itemsource时遇到此错误 在使用ItemsSource之前,Items集合必须为空

我检查了很多解决方案,但我似乎找不到解决这个问题的方法。以下是我的代码片段:

XAML:

C-我的数据库查询:

 public List<Category> Get_Categories()
    {

            using (var context = new ProcessDatabaseEntities())
            {
                return context.Category.ToList();
            }

    }

Category只有两个属性,Name和ID。我知道在分配itemsource列表时,它不是空的。所以我的XAML代码可能有问题。提前谢谢你

我相信你的问题很常见。基本上,您不能同时使用TreeView.ItemsSource和TreeView.Items属性。。。你必须选择一种方式。通常,这个问题会表现出来,因为开发人员做了这样的事情…:

<TreeView Name="TreeView" ItemsSource="{Binding SomeCollection}" ... />
这种情况下的解决方案是操作数据绑定集合而不是TreeView。Items集合:

SomeCollection.Add(someItem);
但是,在您的情况下,如果没有看到代码就有点难以猜测,您可能已经完成了第二部分,首先设置或操作了Items属性,然后尝试设置ItemsSource属性。你的解决方案是一样的。。。使用一种方法编辑项目,或使用另一种方法。。。不是两者都有。

treeview\u LoadOnDemand方法在哪里
 public List<Category> Get_Categories()
    {

            using (var context = new ProcessDatabaseEntities())
            {
                return context.Category.ToList();
            }

    }
<TreeView Name="TreeView" ItemsSource="{Binding SomeCollection}" ... />
TreeView.Items.Add(someItem);
SomeCollection.Add(someItem);