小编Ant*_*nyW的帖子

在DataTemplate中绑定CollectionViewSource

'ContentTemplate'是一个DataTemplate,它显示一个具有成员'FooList'(ObservableCollection)的对象.

<DataTemplate x:Key="ContentTemplate">
    <ListBox ItemsSource="{Binding Path=FOO}">
        ...
    </ListBox>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

我需要能够使用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}}">
Run Code Online (Sandbox Code Playgroud)

我从中得到的错误是:

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement.BindingExpression:路径= FooList; 的DataItem = NULL; target元素是'CollectionViewSource'(HashCode = 52991666); target属性是'Source'(类型'Object')

这听起来像是在寻找CollectionViewSource上的'FooList'而不是绑定到DataTemplate的对象.

那么......我怎么才能看到正确的物体呢?

data-binding wpf datatemplate collectionviewsource

12
推荐指数
2
解决办法
5968
查看次数