在 DataTemplate 中绑定 CollectionViewSource [英] Binding a CollectionViewSource within a DataTemplate

查看:25
本文介绍了在 DataTemplate 中绑定 CollectionViewSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

'ContentTemplate' is a DataTemplate that displays an object which has a member 'FooList' (an ObservableCollection).

<DataTemplate x:Key="ContentTemplate">
    <ListBox ItemsSource="{Binding Path=FOO}">
        ...
    </ListBox>
</DataTemplate>

我需要能够使用 CollectionViewSource 过滤该 FooList.这通常是直截了当的,但我似乎无法让绑定在 DataTemplate 中工作.我试图这样做:

I need to be able to filter that FooList using a CollectionViewSource. This is usually been straight forward but I can't seem to get the binding to work within a DataTemplate. I attempted to this:

<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:Path=FooList;数据项=空;目标元素是CollectionViewSource"(HashCode=52991666);目标属性是源"(类型对象")

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

Which sounds to me like it's looking for 'FooList' on the CollectionViewSource instead of the object bound to the DataTemplate.

那么……我如何让它看到正确的对象?

So... how do I get this to look at the correct object?

推荐答案

据我所知,DataTemplate 充当关于将什么插入到可视化树中的指令,但不会成为可视化树本身的一部分.我是在遇到你上面描述的同样问题后才得出这个假设的.我通过将 CollectionViewSource 附加到一个元素的资源来解决这个问题,该元素将成为可视化树的一部分,在我的例子中是一个网格.这是确实有效的示例:

As I understand it, the DataTemplate acts as instructions on what to insert into the visual tree but does not become a part of the visual tree itself. I only came to this hypothesis after running into the same problem you've described above. I fixed the issue by attaching the CollectionViewSource to the resources of an element that would be part of the visual tree, in my case a grid. Here is the sample that did work:

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

这篇关于在 DataTemplate 中绑定 CollectionViewSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆