我试图将我的 DataGrid 列绑定(bind)到可以使用索引器检索列的项目的列表。索引器类型是 DateTime。
我正在使用代码创建 DataGrid 列,并希望创建一个绑定(bind)以从列表中检索值。在 XAML 中,路径将被写为:
{ 绑定(bind)路径 = 值 [01/01/2011] }
但由于我在后面的代码中执行此操作,我需要使用 PropertyPath 定义路径,如下所示:
new Binding{
Path = new PropertyPath("Values[01/01/2011]")
}
构造函数还有另一个重载,它采用路径和参数数组。根据documentation这些参数用于索引器。但是当我把我的绑定(bind)写成
new Binding {
Path = new PropertyPath("Values", new DateTime(2011, 01, 01))
}
绑定(bind)无法解析路径。很公平,我并不是说它应该寻找索引器。但如果我把它写成:
新绑定(bind){
Path = new PropertyPath("Values[]", new DateTime(2011, 01, 01))
}
然后将 DateTime.MinValue 传递给索引器。
有人可以向我解释如何在构造函数中使用 PathParameters 以及如何绑定(bind)到索引器而无需对实际路径中的值执行 ToString 吗?
最佳答案
基于此MSDN文章中,您需要包含“(0)”以指示应放置参数的位置。所以以下应该工作:
new Binding {
Path = new PropertyPath("Values[(0)]", new DateTime(2011, 01, 01))
}
关于wpf - 构造函数中的 PropertyPath 和 PathParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5169767/