Ant*_*nyW 12 data-binding wpf 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的对象.
那么......我怎么才能看到正确的物体呢?
Ada*_*nda 26
据我所知,DataTemplate充当了插入可视树的内容的指令,但不会成为可视树本身的一部分.我遇到了你上面描述的同样的问题,我才得出这个假设.我修复了这个问题,方法是将CollectionViewSource附加到一个元素的资源中,该元素将成为可视树的一部分,在我的例子中是一个网格.以下是完成工作的示例:
<DataTemplate DataType="{x:Type TypedLists:AssetModelListViewModel}">
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="items"
Source="{Binding}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="AssetType.AssetCategory.Name" />
<scm:SortDescription PropertyName="AssetType.Name" />
<scm:SortDescription PropertyName="Manufacturer.Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ListView ItemsSource="{Binding Source={StaticResource items}}">
</ListView>
</Grid>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
Luk*_*sky -1
我认为你需要绑定到以下视图CollectionViewSource:
<ListBox ItemsSource="{Binding Path=View, Source={StaticResource CVS}}">
Run Code Online (Sandbox Code Playgroud)