将CollectionViewSource中的属性绑定到ViewModel的属性 [英] Binding a property inside CollectionViewSource to the ViewModel's Property

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

问题描述

我在这里有一个新的问题。



我有一个DataGrid和一个TextBox。我想根据TextBox的值过滤DataGrid。



我已经使用 MarkupExtensions a href =http://stackoverflow.com/a/6462282/2284240>这里。



现在它的价值属性(Property)的PropertyFilter类,如上所述链接)是XAML中提到的一个字符串。当我改变它绑定它停止工作。这是我的XAML绑定:

 < CollectionViewSource x:Key =GroupsViewSourceSource ={Binding Groups}> ; 
< CollectionViewSource.Filter>
< me:Filter>
< me:PropertyFilter PropertyName =GroupName
Value ={Binding SearchGroupName}/>
< / me:Filter>
< /CollectionViewSource.Filter>
< / CollectionViewSource>

SearchGroupName是ViewModel中类型为字符串的简单属性。



我还尝试更改绑定如下:

  Value ={Binding DataContext.SearchGroupName, RelativeSource = {RelativeSource 
Mode = FindAncestor,AncestorType = {x:Type UserControl}}}

我已经尝试使用System.Diagnostics进行调试,如下所示:

 < CollectionViewSource x:Key =GroupsViewSource源={Binding Groups}> 
< CollectionViewSource.Filter>
< me:Filter>
< me:PropertyFilter PropertyName =GroupName
Value ={Binding SearchGroupName,diag:PresentationTraceSources.TraceLevel = High}/>
< / me:Filter>
< /CollectionViewSource.Filter>
< / CollectionViewSource>

但是我收到一个编译错误:未知属性PresentationTraceSources.TraceLevel for System.Windows.Data.Binding ...



我认为我与RelativeSource的绑定不起作用,因为我认为CollectionViewSource不是Visual / Logical Tree的成员。



所以我认为我的DataContext可能是空的。如果你处于相同的情况,你们喜欢什么解决方案?

解决方案

你可以尝试移动使用诸如 Path = DataContext。< property-name>,Source = {...}的绑定,将正确的 DataContext 过滤到框架元素的资源中x:参考< element-name>} 。使用 StaticResource 引用过滤器。



此解决方法对于收集容器中的绑定 for






代码应该是这样的(未经测试):

 < SomeElement Name =el> 
< SomeElement.Resources>
< me:PropertyFilter x:Key =Filter1PropertyName =GroupName
Value ={Binding DataContext.SearchGroupName,Source = {x:Reference el}}/>
< CollectionViewSource x:Key =GroupsViewSourceSource ={Binding Groups}>
< CollectionViewSource.Filter>
< me:Filter>
< StaticResource ResourceKey =Filter1/>
< / me:Filter>
< /CollectionViewSource.Filter>
< / CollectionViewSource>


I am here with a new problem.

I have a DataGrid and a TextBox. I would like to filter the DataGrid based on the TextBox's value.

I have done that using MarkupExtensions as mentioned here.

Now it works fine upto the Value property (Property of PropertyFilter class as mentioned in above link) is a string mentioned in XAML. When I change it to binding it stops working. Here is my XAML with binding:

<CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
    <CollectionViewSource.Filter>
        <me:Filter>
            <me:PropertyFilter PropertyName="GroupName"
                               Value="{Binding SearchGroupName}" />
        </me:Filter>
    </CollectionViewSource.Filter>
</CollectionViewSource>

SearchGroupName is a simple Property of type string in my ViewModel.

I have also tried to change the binding as follows:

Value = "{Binding DataContext.SearchGroupName, RelativeSource={RelativeSource 
                               Mode=FindAncestor, AncestorType={x:Type UserControl}}}"

I have tried to debug it using System.Diagnostics as follows:

<CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
    <CollectionViewSource.Filter>
        <me:Filter>
            <me:PropertyFilter PropertyName="GroupName"
                               Value="{Binding SearchGroupName, diag:PresentationTraceSources.TraceLevel=High}" />
        </me:Filter>
    </CollectionViewSource.Filter>
</CollectionViewSource>

But then I get a compilation error : unknown property PresentationTraceSources.TraceLevel for System.Windows.Data.Binding......

I think my binding with RelativeSource does not work because I think that CollectionViewSource is not a member of Visual/Logical Tree.

Thus I think my DataContext might be null. What are the solutions that you guys prefer if you are in the same situation?????

解决方案

You can try moving the filter into the resources of the framework element with the correct DataContext, using a binding like Path=DataContext.<property-name>, Source={x:Reference <element-name>}. Reference the filter where you need it using StaticResource.

This workaround is useful for binding in collection containers for example.


Code should be something like this (untested):

<SomeElement Name="el">
    <SomeElement.Resources>
        <me:PropertyFilter x:Key="Filter1" PropertyName="GroupName"
                           Value="{Binding DataContext.SearchGroupName, Source={x:Reference el}}" />
        <CollectionViewSource x:Key="GroupsViewSource" Source="{Binding Groups}">
            <CollectionViewSource.Filter>
                <me:Filter>
                    <StaticResource ResourceKey="Filter1"/>
                </me:Filter>
            </CollectionViewSource.Filter>
        </CollectionViewSource>

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

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