【问题标题】:Binding a property inside CollectionViewSource to the ViewModel's Property将 CollectionViewSource 中的属性绑定到 ViewModel 的属性
【发布时间】:2015-01-25 06:05:24
【问题描述】:

我遇到了一个新问题。

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

我已经使用MarkupExtensions 完成了这项工作,正如提到的here

现在它可以正常工作,直到 Value 属性(上面链接中提到的 PropertyFilter 类的属性)是 XAML 中提到的字符串。当我将其更改为绑定时,它停止工作。这是我的带有绑定的 XAML:

<CollectionViewSource x:Key="GroupsViewSource" Source="{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" Source="{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 可能为空。如果你们遇到同样的情况,你们更喜欢什么样的解决方案????

【问题讨论】:

    标签: c# wpf xaml data-binding


    【解决方案1】:

    您可以尝试使用Path=DataContext.&lt;property-name&gt;, Source={x:Reference &lt;element-name&gt;} 之类的绑定将过滤器移动到具有正确DataContext 的框架元素的资源中。使用StaticResource 在需要的地方引用过滤器。

    例如,此解决方法对binding in collection containers 很有用。


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

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

    【讨论】:

    • 我按照你的建议使用了x:Reference。现在我的绑定是正确的,因为我放置了 ValueConverter 并在其中放置了一个断点,它显示了预期值。但是当我在文本框中写任何东西时,dataGrid 不会被过滤。我还将 UpdateSourceTrigger 更改为 PropoertyChanged。但是当我点击 DataGrid 的列标题时,过滤器起作用了。
    • CollectionViewSource 无法知道过滤事件方法的结果已更改,您需要以某种方式对其进行更新。虽然不知道怎么做...
    • 谢谢。然后我必须找到另一种过滤数据的方法。
    • @Vishal:你找到方法了吗?
    • @stalbaig 几年前我离开了 WPF。所以,我不记得了。对不起!我的朋友!
    猜你喜欢
    • 2011-03-01
    • 2011-08-26
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2012-07-20
    相关资源
    最近更新 更多