0

Given a datagrid with a DataGridCheckBoxColumn bound to a boolean object

<datagrid .....>
<DataGrid.Columns>
   <DataGridCheckBoxColumn  Header="Issues"  Binding="{Binding HasIssue,UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>

How can I programmatically obtain the Binding Expression to be able to call UpdateTarget() ?

ie.

   var expression = datagrid1.GetBindingExpression(DataGrid.**WhatProperty**);
    if (expression != null)
       expression .UpdateTarget();

I have also tried

var expression = BindingOperations.GetBindingExpression(datagrid1, ***`WhatDependencyObjectHere`***);
1
  • What do you mean by getting the binding expression? (in the code-behind) Commented May 27, 2020 at 8:34

1 Answer 1

2

To answer your exact question, you would have to get the actual CheckBox that the DataGridCheckBoxColumn is generating. Here's an example function. I don't know what type of collection your ItemsSource is, so I called mine TestObject and set ItemsSource to an IList<TestObject>.

static void UpdateBindingTarget(DataGrid dg, DataGridCheckBoxColumn col, TestObject item)
{
    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromItem(item);
    CheckBox cb = (CheckBox)col.GetCellContent(row);
    var be = cb.GetBindingExpression(CheckBox.IsCheckedProperty);
    if (be != null) { be.UpdateTarget(); }
}

The real question is why you would want to do this in the first place. The above is not what I would consider good practice, more of a hacky workaround. If you need your binding source to update the target, it should either inherit DependencyObject and use a DependencyProperty, or implement INotifyPropertyChanged and raise the PropertyChanged event.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.