【问题标题】:CompositeCollection + CollectionContainer: Bind CollectionContainer.Collection to property of ViewModel that is used as DataTemplates DataTypeCompositeCollection + CollectionContainer:将 CollectionContainer.Collection 绑定到用作 DataTemplates DataType 的 ViewModel 的属性
【发布时间】:2013-10-15 02:31:04
【问题描述】:

我没有正确的绑定语法来访问 MyViewModel 中的 MyViewModelCatsDogs 属性,该 DateTemplate 在其资源中定义了 CompositeCollection

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>

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

【问题讨论】:

    标签: c# wpf data-binding datatemplate compositecollection


    【解决方案1】:

    由于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>
    

    编辑:CompositeCollection 类不是从FrameworkElement 派生的,因此没有DataContext 属性来支持数据绑定。仅当您使用Binding 提供Source 时,它才会起作用。在这里查看https://stackoverflow.com/a/6446923/1254795 了解更多信息。

    【讨论】:

    • 这对我很有用。但如果您能解释为什么这是必需的,那将会更有帮助。我不明白:1) 为什么我不能以相同的方式声明CollectionContainer 资源并直接使用它,而不是通过CollectionViewSource 间接使用? 2) 为什么我不能将CompositeCollection 声明为资源并直接绑定到CollectionContainer 对象? CollectionViewSource 有什么特别之处,即使其他类型不起作用,它也能在这里工作?
    【解决方案2】:

    尝试为您的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>
    

    【讨论】:

    • 这不起作用。我得到一个异常,ListBox 的名称无法解析。我认为这是因为CompositeCollectionDataTemplate.Resources 中声明,ListBox 在之后声明。
    • 只是一个建议..你为什么要在模板资源中收集..你可以在 ListBox 资源中移动它吗?
    • 如果我将CompositeCollection 移动到ListBox.Resources 那么我如何将其引用为ListBox.ItemsSource
    • 而不是StaticResource使用{DynamicResource MyColl}
    • 如果您将 collectin 放入列表框资源并使用 dynamicresource 来引用它,它不会给出周期性错误...刚刚测试过这个...工作正常..用测试代码更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2017-08-08
    • 2015-01-25
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多