WPF DataGrid:如何获取单元格的绑定表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15216362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:52:23  来源:igfitidea点击:

WPF DataGrid: How to get binding expression of a cell?

wpfbindingdatagrid

提问by newman

I need to get access to the binding expression of the DataGrid cell in a DataGridTextColumn. For example:

我需要访问 DataGridTextColumn 中 DataGrid 单元格的绑定表达式。例如:

        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

I managed to get the TextBlock associated with the cell:

我设法获得了与单元格关联的 TextBlock:

        var cell = dataGrid.GetCellCtrl<TextBlock>(dataGrid.CurrentCell);

And cell seems to be correct. I can call

细胞似乎是正确的。我可以打电话

        cell.SetValue(TextBlock.TextProperty, value);

To update the cell text. It seems to be working on the grid (number updated). However, as I realize after a while, the source doesn't get updated. It didn't help even if I turn UpdateSourceTrigger to PropertyChange. Then, I thought I need to get the binding expression and call UpdateSource explicitly.

更新单元格文本。它似乎在网格上工作(数字已更新)。然而,正如我在一段时间后意识到的那样,源代码没有得到更新。即使我将 UpdateSourceTrigger 转换为 PropertyChange,它也没有帮助。然后,我想我需要获取绑定表达式并显式调用 UpdateSource。

        var bindingExpr = cell.GetBindingExpression(TextBlock.TextProperty);

but bindingExpr is always null. Why?

但 bindingExpr 始终为空。为什么?

EDIT: The original problem I had was that I can get to the binding TextBlock for the cell, and set the TextBlock.TextProperty. However, the source doesn't get updated. This is something I'm trying to resolve this problem.

编辑:我遇到的最初问题是我可以访问单元格的绑定 TextBlock,并设置 TextBlock.TextProperty。但是,源不会更新。这是我正在尝试解决此问题的方法。

回答by sa_ddam213

The TextBoxin the DataGridTextColumnwill not have a binding expression the column itself has the binding.

TextBoxDataGridTextColumn不会有绑定表达式列本身所具有的约束力。

DataGridTextColumnis derived from DataGridBoundColumnwhich uses a BindingBaseproperty not TextBlock.TextProperty, However the Bindingproperty is not a DependancyPropertyso you will have to access using normal public properties.

DataGridTextColumn派生自DataGridBoundColumn它使用的BindingBase属性 not TextBlock.TextProperty,但是该Binding属性不是 aDependancyProperty因此您必须使用普通的公共属性进行访问。

So you will have to do a bit of casting as the Bindingproperty in DataGridTextColumnis type BindingBase.

因此,您将不得不进行一些转换,因为Bindingin 中的属性DataGridTextColumn是 type BindingBase

Something like this should work (untested)

像这样的东西应该可以工作(未经测试)

var binding = (yourGrid.Columns[0] as DataGridBoundColumn).Binding as Binding;

回答by Max Pedro

TextBox t = e.EditingElement as TextBox; string b= t.GetBindingExpression(TextBox.TextProperty).ResolvedSourcePropertyName;

TextBox t = e.EditingElement as TextBox; 字符串 b= t.GetBindingExpression(TextBox.TextProperty).ResolvedSourcePropertyName;