如何将单元格值绑定到对象的特定属性?
我有一个在单元格级别填充以下类型的 DataTable
:
public class GridCell
{
public string Value { get; set}
public Guid Id { get; set; }
}
我已将 DataTable 绑定到 WPF Toolkit DataGrid,但单元格为空。如何告诉 DataGrid
在我的 GridCell
类型的 Value
属性中查找单元格内容?
绑定片段:
<DataGrid ItemsSource="{Binding SelectedItem.Table}" />
I have a DataTable
that is populated at the cell level with the following type:
public class GridCell
{
public string Value { get; set}
public Guid Id { get; set; }
}
I've bound the DataTable to a WPF Toolkit DataGrid, but the cells are coming up empty. How do I tell the DataGrid
to look in the Value
property of my GridCell
type for the cell contents?
Binding snippet:
<DataGrid ItemsSource="{Binding SelectedItem.Table}" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您问题中的有限信息,我猜测您的数据表包含我不推荐的自定义类型列,因为它会使您的代码变得更加复杂。但如果您决定这样做,您可以实现自定义列类型来处理该问题。我还没有尝试过,但这个链接看起来很有希望: 以编程方式使用 DataGridTemplateColumn 将 DataTable 绑定到 WPF DataGrid
您还可以使用值转换器来实现相同的目的。下面的示例将列索引作为参数传递,但您也可以使用某种格式传递您感兴趣的属性(例如 0-Value,表示第 0 列,属性值)。
XAML:
隐藏代码:
From the limited info in your question I am guessing that your data table contains columns of custom types which I would not recommend since it will make your code so much more complicated. But if you decide to go that way you can implement your custom column type to handle that. I haven't tried that, but this link looks promising: Bind DataTable to WPF DataGrid using DataGridTemplateColumn Programatically
You can also use value converters to achieve the same thing. Below sample pass the column index as parameter but you could also pass which property you are interested in as well using some format (0-Value for example, meaning column 0, property Value).
XAML:
Code behind:
尝试
ItemsSource="{Binding Path=GridCellList}"
。Try
ItemsSource="{Binding Path=GridCellList}"
.