How to bind column header to property in ViewModel? (WPF MVVM)

后端 未结 1 1525
情书的邮戳
情书的邮戳 2020-12-16 23:05

I Have window that have DataContext that is bind to ViewModel object (VM1 for the example). VM1 have a lot of properties and one of them is a string that called \"MyTitle\".

相关标签:
1条回答
  • 2020-12-16 23:26

    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}}"/>
    
    0 讨论(0)
提交回复
热议问题