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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19243109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:48:23  来源:igfitidea点击:

CompositeCollection + CollectionContainer: Bind CollectionContainer.Collection to property of ViewModel that is used as DataTemplates DataType

c#wpfdata-bindingdatatemplatecompositecollection

提问by Oliver

I do not get the correct Binding syntax to access the Catsand Dogsproperties of MyViewModelwithin a DateTemplatethat defines a CompositeCollectionwithin its resources.

我没有获得正确的绑定语法来访问在其资源中定义 a 的a中的CatsDogs属性。MyViewModelDateTemplateCompositeCollection

public class MyViewModel
{
    public ObservableCollection<Cat> Cats { get; private set; }
    public ObservableCollection<Dog> Dogs { get; private set; }
}
<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>

What do I have to insert for ???? to bind the Dogsand Catscollections to the CollectionContainers?

我必须插入什么???将DogsCats集合绑定到CollectionContainers?

回答by Oliver

Due to the issue with data binding on CollectionContaineras described http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesource?forum=wpfI now use the following approach:

由于数据绑定问题CollectionContainer,如http://social.msdn.microsoft.com/Forums/vstudio/en-US/b15cbd9d-95aa-47c6-8068-7ae9f7dca88a/collectioncontainer-does-not-support-relativesource? 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>

Edit:The CompositeCollectionclass does not derive from FrameworkElementand thus does not have a DataContextproperty to support data binding. It will only work if you use Bindingproviding a Source. Have a look here https://stackoverflow.com/a/6446923/1254795for more information.

编辑:CompositeCollection类不从派生FrameworkElement,因此不具有DataContext财产支持数据绑定。仅当您使用Binding提供Source. 看看这里https://stackoverflow.com/a/6446923/1254795了解更多信息。

回答by Nitin

Try giving name to your ListBox and refer its DataContext in binding:

尝试为您的 ListBox 命名并在绑定中引用它的 DataContext:

   <ListBox x:Name="myList" ItemsSource="{DynamicResource MyColl}">
        <ListBox.Resources>
            <CompositeCollection x:Key="MyColl">

                 <CollectionContainer Collection="{Binding DataContext.Dogs, Source={x:Reference myList}}"/>
                <CollectionContainer Collection="{Binding DataContext.Cats, Source={x:Reference myList}}"/>
            </CompositeCollection>
        </ListBox.Resources>
    </ListBox>