CompositeCollection + CollectionContainer:将CollectionContainer.Collection绑定到ViewModel的属性,该属性用作DataTemplates DataType

Oli*_*ver 14 c# data-binding wpf datatemplate compositecollection

我没有得到正确的绑定语法访问CatsDogs属性MyViewModelDateTemplate定义了CompositeCollection它的资源范围内.

public class MyViewModel
{
    public ObservableCollection<Cat> Cats { get; private set; }
    public ObservableCollection<Dog> Dogs { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
<DataTemplate DataType={x:Type local:MyViewModel}">
  <DataTemplate.Resources>
    <CompositeCollection x:Key="MyColl">
      <!-- How can I reference the Cats and Dogs properties of MyViewModel? -->
      <CollectionContainer Collection="{Binding Dogs, ????}">
      <CollectionContainer Collection="{Binding Cats, ????}">
    </CompositeCollection>
  </DataTemplate.Resources>
  <ListBox ItemsSource="{StaticResource MyColl}">
    <!-- ... -->
  </ListBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

我要为????插入什么?绑定DogsCats集合到CollectionContainers?

Oli*_*ver 41

由于http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesourceCollectionContainer所描述的数据绑定问题? forum = wpf我现在使用以下方法:

<ListBox>
  <ListBox.Resources>
    <CollectionViewSource x:Key="DogCollection" Source="{Binding Dogs}"/>
    <CollectionViewSource x:Key="CatCollection" Source="{Binding Cats}"/>
  </ListBox.Resources>
  <ListBox.ItemsSource>
    <CompositeCollection>
      <CollectionContainer Collection="{Binding Source={StaticResource DogCollection}}"/>
      <CollectionContainer Collection="{Binding Source={StaticResource CatCollection}}"/>
    </CompositeCollection>
  </ListBox.ItemsSource>
  <!-- ... -->
</ListBox>
Run Code Online (Sandbox Code Playgroud)

编辑:CompositeCollection类不从派生FrameworkElement,因此不具有DataContext财产来支持数据绑定.它只会在你使用Binding提供的时候起作用Source.有关更多信息,请查看/sf/answers/451284641/.