5
votes

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

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

I managed to get the TextBlock associated with the cell:

        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.

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

but bindingExpr is always null. Why?

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.

3
Hi, Have you solved the problem ?mohammad jannesary
SortMemberPath for the column will by default contains the Binding Path (this is default property value to enable sorting by bound property).It is not reliable and intended way to get the binding path and will not work for multiple bindings or when SortMemberPath is manually set.kiran

3 Answers

6
votes

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

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

So you will have to do a bit of casting as the Binding property in DataGridTextColumn is type BindingBase.

Something like this should work (untested)

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

You need to find the TextBlock:

var textBlock = cell.FindVisualChild<TextBlock>();
BindingExpression bindingExpression = textBlock.GetBindingExpression(
  TextBlock.TextProperty);

Code for FindVisualChild():

public static class DependencyObjectExtensions
{
    [NotNull]
    public static IEnumerable<T> FindVisualChildren<T>([NotNull] this DependencyObject dependencyObject)
        where T : DependencyObject
    {
        if (dependencyObject == null) throw new ArgumentNullException(nameof(dependencyObject));

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
            if (child is T o)
                yield return o;

            foreach (T childOfChild in FindVisualChildren<T>(child))
                yield return childOfChild;
        }
    }

    public static childItem FindVisualChild<childItem>([NotNull] this DependencyObject dependencyObject)
        where childItem : DependencyObject
    {
        if (dependencyObject == null) throw new ArgumentNullException(nameof(dependencyObject));

        foreach (childItem child in FindVisualChildren<childItem>(dependencyObject))
            return child;
        return null;
    }
}
-2
votes

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