views:

94

answers:

1

I have a datagrid in which i manually specify the columns (AutoGenerateColumns="false"). I'm using MVVM and I would like to bind the column header (the text) so that I get it from my ViewModel. But I can't find a way to do that.

The closest thing I've found is this: http://stackoverflow.com/questions/652240/wpf-toolkit-bind-datagrid-column-header-to-dynamicresource ...which is a "trick" with which you can bind the column header to a string statically declared in XAML. But I need to get it from my ViewModel.

The reason I want to this is that the text I want to use as column header will be shown in several places throughout the GUI. To get make sure it actually is the same everywhere I want to have it available in code. And I can't store the strings in a XAML resource file because I also need access to these strings in code.

I'm using a RadGridView from TeleRik, but I guessing I'd have the same problem if I the standard gridview.

Any tips appreciated! Thanks!

+1  A: 

Here is a suggestion, which I have not tried, sorry.

In Silverlight, I tried the following:

<DataGridTextColumn Header="{Binding ElementName=LayoutRoot, Path=DataContext.MyProperty}" ... />

(LayoutRoot is the first control in the window/user control, MyProperty is something in the VM) It does not work because the column has no access to the visual tree.

It may be worth a try in WPF with your grid.

Timores
It works! Thank you very much Timores!! :)
haagel