【问题标题】:wpf collections subproperty data binding pathwpf 集合子属性数据绑定路径
【发布时间】:2025-12-19 09:10:09
【问题描述】:

我正在尝试实现与下面显示的 xpath 函数等效的功能,但使用 wpf 绑定路径。本质上,我需要仅使用人的 ID 作为过滤器来获取集合列表中的人的姓名。我不太确定如何为此正确构建 wpf 路径。

我浏览了一些 msdn 帖子,尤其是这个:binding_to_collections,但据我所知,它只描述了基于索引的过滤。

//XPath that works with xmls
string xpath = "//People[ID='123456']/Name";

//My attempts at wpf path bindings
var binding = new Binding($"People[ID='123456'].Name");
var binding = new Binding($"People[ID='{personID}'].Name");
var binding = new Binding($"People[0].Name");//works but doesn't show correct person

【问题讨论】:

    标签: c# wpf data-binding observablecollection


    【解决方案1】:

    恐怕 WPF 或 XAML 中的属性绑定路径不支持 ID='123456'。索引和编译时常量是唯一的选项。

    例如,如果 PeopleDictionary<int, Person>,您可以基于 constant 键 (123456) 绑定到特定的 Person,如下所示:

    People[123456].Name
    

    但是您不能使用绑定路径根据属性进行过滤。这不受支持。

    【讨论】:

    • 感谢您的回答,您知道这是否适用于字符串字典吗?
    • @5tar-Kaster:是的,应该的。