我需要在每个组内部创建一个组框列表时,会有一个复选框列表,所有复选框都是动态加载的。 我创建了vm,将所有复选框插入所有组框:
ObservableCollection<GroupBox> Groups = new ObservableCollection<GroupBox>();
GroupBox group = new GroupBox();
ObservableCollection<CheckBox> content= new ObservableCollection<CheckBox>();
group.content = content;
Content.Add(new checkBox());
Content.Add(new checkBox());
.
.
.
我的xaml看起来像这样:
<ItemsControl ItemSource = "{Binding Groups}">
<ItemsControl ItemSource = "{Binding Content}">
</ItemsControl
</ItemsControl>
如果我只绑定到组:我得到了很好的组框,但是当我在其中添加itemsControl时,它不起作用。
我得到:“在使用ItemsSource之前,项目集合必须为空”
我哪里出错了? TX Tal
答案 0 :(得分:0)
如果我只绑定到组:我得到了很好的组框,但是当我在里面添加itemsControl时,它不起作用。 我得到:&#34;在使用ItemsSource&#34;
之前,项目集合必须为空这是因为内部ItemsControl被视为外部ItemsControl的子项,因此当您尝试设置ItemsSource时会抛出异常。
对于多级,你需要将DataTemplate设置为o_weisman所说的或类似,
<ItemsControl ItemsSource="{Binding Groups}">
<ItemsControl.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Content}">
<!--control you need to display-->
</HierarchicalDataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>