How to associate a column heading with a property in a ViewModel? (WPF MVVM)

I have a window with a DataContext attached to a ViewModel (e.g. VM1). VM1 has many properties, and one of them is a string called MyTitle.

I have a DataGridTextColumn in 'Window \ Grid \ DataGrid'. How do I bind the Title property in a DataGridTextColumn to the MyTitle property in my VM1 ViewModel?

Thanks!

+4
source share
1 answer

Unfortunately, DataGrid column definitions do not inherit DataContext because they are not part of the visual tree, so you cannot directly bind to the ViewModel. You need to turn to a workaround, for example, described in this article :

 <DataGrid.Resources> <local:BindingProxy x:Key="proxy" Data="{Binding}" /> </DataGrid.Resources> ... <DataGridTextColumn Header="{Binding Data.MyTitle, Source={StaticResource proxy}}"/> 
+16
source

Source: https://habr.com/ru/post/1484855/


All Articles