'ContentTemplate' 是一个 DataTemplate,用于显示一个具有 'FooList' 成员(一个 ObservableCollection)的对象。
<DataTemplate x:Key="ContentTemplate">
<ListBox ItemsSource="{Binding Path=FOO}">
...
</ListBox>
</DataTemplate>
我需要使用CollectionViewSource对FooList进行过滤。这通常很简单,但在DataTemplate中似乎无法使绑定起作用。我尝试了以下内容:
<DataTemplate x:Key="ContentTemplate">
<DataTemplate.Resources>
<CollectionViewSource x:Key="CVS" Source="{Binding Path=FooList}" Filter="FooFilter"/>
<DataTemplate.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource CVS}}">
我遇到的错误是:
System.Windows.Data Error: 2 : 找不到目标元素的主 FrameworkElement 或 FrameworkContentElement。BindingExpression:Path=FooList; DataItem=null; target element is 'CollectionViewSource' (HashCode=52991666); target property is 'Source' (type 'Object')
这听起来像是它在 CollectionViewSource 上查找 'FooList',而不是绑定到 DataTemplate 的对象。
那么...我该如何让它查找正确的对象呢?