haagel
haagel

Reputation: 2728

WPF: Binding DataGrid column header to ViewModel

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: 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!

Upvotes: 1

Views: 4680

Answers (1)

Timores
Timores

Reputation: 14589

In Silverlight I have tried the following:

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

(LayoutRoot is the first control in the Window/UserControl, 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.

Upvotes: 2

Related Questions