Wpf – Dynamically change the WPF Datagrid column header

data-bindingdatagridwpf

I am using wpf toolkit datagrid in wpf win applications. In my view model, I have a observablecollection property which is binded to the itemsource of the datagrid.
Student class is having Name, Age, Class properties and displays in each columns in the grid.

But I have a property called "Header1" in my View model class and how i can bind it to the Header of the data grid text column ? When I used, it is not displaying the header string in the grid. I need to update this column header at run time. How I can do that ? I also tried the following way;

<dg:DataGridColumn Header = "{Binding Header1, ElementName=MyUsrCtrl}" />

BUT this is also not working ? I used DataContext.Header1, ElementName=MyUsrCtrl also…but no use.

How it is possible ?

Best Answer

You cannot bind to any daatgrid columns as they are not part of the visual tree. Use the proxy element trick.

WPF Error: Cannot find governing FrameworkElement for target element

Related Topic