WPF DataGrid: how to get cell expression binding?

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

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

I managed to get the associated TextBlock cell:

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

And the cell seems right. I can call

  cell.SetValue(TextBlock.TextProperty, value); 

To update cell text. It seems to be working on a grid (number updated). However, as I understand it, after a while, the source is not updated. This did not help even if I return the UpdateSourceTrigger to the PropertyChange. Then I thought that I needed to get the binding expression and explicitly call UpdateSource.

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

but bindingExpr is always null. Why?

EDIT: The initial problem I ran into was that I can go to bind TextBlock to a cell and set TextBlock.TextProperty. However, the source is not updated. This is what I'm trying to solve this problem.

+4
source share
2 answers

TextBox in the DataGridTextColumn will not have a binding expression in which the column itself has a binding.

DataGridTextColumn derived from a DataGridBoundColumn , which uses the BindingBase property not TextBlock.TextProperty . However, the Binding property is not DependancyProperty , so you will have to access using the usual public properties.

So, you will need to do a little casting since the Binding property in the DataGridTextColumn is a type of BindingBase .

Something like this should work (unverified)

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

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

-1
source

All Articles