wpf - 构造函数中的 PropertyPath 和 PathParameters

标签 wpf binding

我试图将我的 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/

相关文章:

cocoa - 在插入时开始编辑

c# - MVVM 中具有动态内容的 WPF 状态栏

c# - 静态变量的 INotifyPropertyChanged

c# - 一个窗口中的 WPF 应用程序

c# - WPF 中的拖放

mvvm - 绑定(bind)到 View 模型 xamarin.forms 中的属性

c# - 将 ListViewColumn 宽度设置为最宽的项目,无论它是否在初始渲染中?

Angular2 将 map 的值绑定(bind)到复选框

c# - 无法将 WPF 菜单绑定(bind)到 ItemsSource

objective-c - Cocoa Binding, View 或 Controller 是接收者有关系吗?