【发布时间】:2015-04-28 10:15:51
【问题描述】:
我正在尝试使用复合集合作为 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 设法直接绑定到视图模型中的集合 - 但我无法分辨示例中的“数据”对象是什么。
【问题讨论】: