'ContentTemplate'是一个DataTemplate,它显示具有成员'FooList'(ObservableCollection)的对象。在DataTemplate中绑定CollectionViewSource
<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错误:2:无法找到目标元素理事FrameworkElement的或FrameworkContentElement上。 BindingExpression:路径= FooList;的DataItem = NULL;目标元素是'CollectionViewSource'(HashCode = 52991666);目标属性是'源'(类型'对象')
这听起来像它寻找'FooList'在CollectionViewSource而不是绑定到DataTemplate的对象。
那么......我怎么才能看到正确的对象呢?
我遇到同样的问题。唯一我能想到的是,CVS不知道绑定的正确来源(你的'FooList'成员),它应该是这个级别的DataContext。有没有办法显式地将CVS上的源设置为DataTemplate的当前DataContext?再次,应该假设,但这不适合我们两个。 (你和我的唯一区别是你绑定到一个内部的ListBox,我绑定到一个HierarchicalDataTemplate的子项目以供在treeview中使用,但除此之外,你和我在同一条船上。 – MarqueIV 2010-10-20 16:45:34
在我看来,它不应该有权访问资源部分中DataTemplate的数据。有谁知道这是否正确? – 2010-12-29 12:02:01