How to bind column header to property in ViewModel? (WPF MVVM)
The accepted answer didn't work for me.
I was able to make this work.
<FrameworkElement x:Name="proxy" DataContext="{Binding}"/>
<DataGridTextColumn Header="{Binding Source={x:Reference proxy}, Path=DataContext.MyTitle}/>
Unfortunately, the column definitions of the DataGrid
don't inherit the DataContext
, because they're not part of the visual tree, so you can't bind directly to the ViewModel. You need to resort to a workaround such as the one described in this article:
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
...
<DataGridTextColumn Header="{Binding Data.MyTitle, Source={StaticResource proxy}}"/>