如何获取datagrid列绑定的字段? [英] How to get field that datagrid column is bound to?
问题描述
在我的xaml中,我修改了每个列标题以包含一个按钮。对于命令参数,我想使用列的数据字段名称,而不是标题内容。例如。我想要的是 JOB_TITLE,而不是标题内容是 Job Title。
In my xaml I have modified each column header to include a button. For the command parameter I would like to use the column's data field name, instead of the header content. E.g. Instead of "Job Title" which is what the header content is, I want "JOB_TITLE".
对于标题内容,我将使用:
For header content I would use:
<Button Command="{Binding DataContext.OpenFilterCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{TemplateBinding Content}">
如何获取实际的字段名称?
How do I get the actual field name?
推荐答案
如果我认为正确,那么您希望将 绑定到该列的绑定属性名称作为命令参数传递给OpenFilterCommand 。
If i assume it right you want binding property name to which column is binded to pass as command parameter to OpenFilterCommand.
假设您的DataGrid的列是这样的:
Suppose columns are like this for your DataGrid:
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding NAME}"/>
<DataGridTextColumn Binding="{Binding JOB_TITLE}" />
</DataGrid.Columns>
,并希望将属性名称 JOB_TITLE
传递为命令参数。可以这样实现:
and want to pass property name JOB_TITLE
as command parameter. This can be achieve like this:
<Button Command="{Binding DataContext.OpenFilterCommand,
RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding Column.Binding.Path.Path,
RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
EXPLANATION
TemplatedParent( DataGridColumnHeader )->列( DataGridTextColumn )
- ->绑定( BindingBase )->路径( PropertyPath )->路径( Actual PropertyName )
TemplatedParent (DataGridColumnHeader) --> Column (DataGridTextColumn) --> Binding (BindingBase) --> Path (PropertyPath) --> Path (Actual PropertyName)
这篇关于如何获取datagrid列绑定的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!