WPF - 如何绑定 DataGridTemplateColumn
我试图获取与特定 DataGridColumn
关联的属性名称,以便我可以基于该名称执行一些操作。当用户单击列标题上的上下文菜单项时,将调用此函数...
这对于开箱即用的现成滚动列类型(如 DataGridTextColumn )来说很好,因为它们是绑定的,但问题是我的一些列是DataGridTemplateColumns
,它们没有绑定。
private void GroupByField_Click (object sender, RoutedEventArgs e){
MenuItem mi = (MenuItem)sender;
ContextMenu cm = (ContextMenu) mi.Parent;
DataGridColumnHeader dgch = (DataGridColumnHeader) cm.PlacementTarget;
DataGridBoundColumn dgbc = (DataGridBoundColumn) dgch.Column;
Binding binding = (Binding) dgbc.Binding;
string BoundPropName = binding.Path.Path;
//Do stuff based on bound property name here...
}
因此,以我的 Name
列为例...它是一个 DataGridTemplateColumn
(因为其中有图像和其他一些内容)。因此,它实际上并未绑定到“Name”属性......但我希望如此,以便上面的代码能够工作。
我的问题实际上分为两部分:
是否可以使
DataGridTemplateColumn
绑定,以便上面的代码可以工作?我可以以某种方式将其绑定到属性吗?或者我是否需要完全不同的东西,并更改上面的代码?
提前致谢!
在
I am trying to get the name of the property associated with a particular DataGridColumn
, so that I can then do some stuff based on that. This function is called when the user clicks context menu item on the column's header...
This is fine for the out-of-the-box ready-rolled column types like DataGridTextColumn
, since they are bound, but the problem is that some of my columns are DataGridTemplateColumns
, which are not bound.
private void GroupByField_Click (object sender, RoutedEventArgs e){
MenuItem mi = (MenuItem)sender;
ContextMenu cm = (ContextMenu) mi.Parent;
DataGridColumnHeader dgch = (DataGridColumnHeader) cm.PlacementTarget;
DataGridBoundColumn dgbc = (DataGridBoundColumn) dgch.Column;
Binding binding = (Binding) dgbc.Binding;
string BoundPropName = binding.Path.Path;
//Do stuff based on bound property name here...
}
So, take for example my Name
column... it's a DataGridTemplateColumn
(since it has an image and some other stuff in there). Therefore, it is not actually bound to the 'Name' property... but I would like to be, so that the above code will work.
My question is two-part, really:
Is it possible to make a
DataGridTemplateColumn
be BOUND, so that the above code would work? Can I bind it somehow to a property?Or do I need to something entirely different, and change the code above?
Thanks in advance!
AT
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管您无法绑定模板列,但您可以绑定该列中包含的控件之一。这就是我解决类似问题的方法:
如果我正确理解了最初的示例,这将意味着更改 GroupByField_Click() 方法的逻辑以检查发送列是否是模板列,然后查看它包含的元素以获得 Binding 对象。
Although you can't bind a template column, you can bind one of the controls held in that column. This is how I solved a similar problem:
If I've understood the initial example properly, this would mean changing the logic of the
GroupByField_Click()
method to check whether the sending column was a template column and then looking at the elements it contained to obtain the Binding object.对我来说,
DataGridTemplateColumn
的ClipboardContentBinding
是一个解决方案:For me,
ClipboardContentBinding
ofDataGridTemplateColumn
is a solution:这是一个棘手的问题。我们通过遍历其祖父 UserControl(我们在 UserControl 内有 DataGrid)来实现绑定,并且 UserControl 绑定到 Presenter(在我们的例子中为 Model)。
在下面的代码中,检查放置在 DataGridTemplateColumn 内的 AutoCompleteBox 的 SelectedItem 属性。
It's a tricky one. We achieved the binding by traversing to its grandparent UserControl (we had DataGrid inside a UserControl) and the UserControl was bound to a Presenter (Model in our case).
In the code below, check the property SelectedItem of AutoCompleteBox placed inside the DataGridTemplateColumn.
您可以使用
dgbc.ClipboardContentBinding;
You may use
dgbc.ClipboardContentBinding;
否,因为
DataGridTemplateColumn
不是从DataGridBoundColumn
继承,因此转换为DataGridBoundColumn
将会失败。要使上述代码正常工作,所有列都必须继承自
DataGridBoundColumn
抽象类。因此,制作自定义派生 Column 类而不是DataGridTemplateColumn
应该可行。您可以简单地填充
字典BoundPropName;
初始化然后执行
var propName = BoundPropName[dgch.Column]
No, because
DataGridTemplateColumn
doesn't inherit fromDataGridBoundColumn
, so the cast toDataGridBoundColumn
would fail.To make above code work all of your columns must inherit from
DataGridBoundColumn
abstract class. So making custom derived Column classes instead ofDataGridTemplateColumn
should work.You could simply populate a
Dictionary<DataGridColumn, string> BoundPropName;
on initialization and then do
var propName = BoundPropName[dgch.Column]