Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change the WPF Datagrid column header

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 ?

like image 528
user904567 Avatar asked Feb 17 '26 09:02

user904567


1 Answers

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

like image 147
WPF-it Avatar answered Feb 20 '26 19:02

WPF-it



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!