我尝试使用复合集合作为ItemsControl的ItemsSource。然后,ItemsControl应根据复合集合中的对象类型显示不同的控件。
根据这条问题:
How do you bind a CollectionContainer to a collection in a view model?
我发现我不能将复合集合直接绑定为ItemsSource,如下所示:
ItemsSource="{Binding CombinedCollection}"因此,我已经在ItemsControl资源中创建了复合集合:
<ItemsControl.Resources>
<CompositeCollection x:Key="cmpcol">
<CollectionContainer Collection="{Binding TimeSlots}"/>
<CollectionContainer Collection="{Binding Items}"/>
</CompositeCollection>
<DataTemplate DataType="{x:Type tg:TimeSlot}">
<tgvw:TimeSlotRect/>
</DataTemplate>
<DataTemplate DataType="{x:Type tg:Item}">
<TextBox></TextBox>
</DataTemplate>
</ItemsControl.Resources>现在如何将复合集合设置为ItemsSource?我似乎不明白其中的语法。我假设它是这样的:
<ItemsControl.ItemsSource>
<!--Something Here-->
</ItemsControl.ItemsSource>顺便说一句,这里的答案是https://stackoverflow.com/a/5473443/2591770设法直接绑定到视图模型中的一个集合--但是我不能说出这个例子中的'Data‘对象是什么。
发布于 2015-02-26 14:36:38
在View Model中创建一个类型为object的集合。并在其中添加您的复合集合。然后将该列表绑定到项控制项源。
发布于 2015-02-27 13:41:10
解决了。
结果是,在视图模型中:
CompositeCollection CombinedCollection = new CompositeCollection();与以下内容不同:
CombinedCollection = new CompositeCollection();我使用了后者,它在以下情况下工作得很好:
ItemsSource="{Binding CombinedCollection}"菜鸟犯错!
https://stackoverflow.com/questions/28735636
复制相似问题