开发者

In WPF, how to clear DataBinding in a DataGrid?

开发者 https://www.devze.com 2023-02-03 19:07 出处:网络
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 som

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?


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;


The following line solved my problem ::

  BindingOperations.ClearAllBindings(dg);


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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号