In WPF, how to clear DataBinding in a DataGrid?

I am using WPFToolKit DataGrid in my application. I have bound the DataGrid to a XMlDocument. The grid displays the Data from XML. I have to remove all the bindings in DataGrid and reset it during some event.

Now my question is how do I remove the DataBinding between DataGrid and XMLDocument.

I have tried something like this ::

dg.SetValue(DataGrid.BindingGroupProperty, null); //doesn't work

What am I doing wrong?

Upvotes: 2

Views: 10883

Answers (4)

user3988148
user3988148

Reputation: 87

The above didn't work for me. What works for me if binding:

dataGrid.ItemsSource = null;
dataGrid.Columns.Clear();
dataGrid.Items.Clear();
dataGrid.Items.Refresh();

If not binding:

dataGrid.Columns.Clear();
dataGrid.Items.Clear();
dataGrid.Items.Refresh();

Upvotes: 0

The following line solved my problem ::

  BindingOperations.ClearAllBindings(dg);

Upvotes: 2

Notter
Notter

Reputation: 588

Try changing the dataGrid.DataContext to null or an empty string.

Upvotes: 0

Rick Sladkey
Rick Sladkey

Reputation: 34240

To undo a binding in WPF, simply set the property that was previously bound to a some other value. In the case of DataGrid, its data is usually bound to the ItemsSource property so setting that to null will remove its previous binding. But if you have any other properties in the DataGrid that are bound, you will have to set those to "unbound" values as well. Which ones will depend on your situation. But in your example the code would be:

dg.ItemsSource = null;

Upvotes: 6

Related Questions