WPF Datagrid view header binding

核能气质少年 提交于 2019-11-28 14:47:56

You can access the parents datacontext using

Binding="{Binding RelativeSource={RelativeSource FindAncestor, 
    AncestorType={x:Type DataGrid}}, Path=DataContext.ColumnName}" 

So to add a column showing A's Property do

<DataGridTextColumn Header="Company" Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Company}"/>
Kurubaran

Finally I managed to get it working. If I directly bind the property to the column header its not populating the value.

I had to do as following:

<DataGridTextColumn Binding="{Binding Name}">
    <DataGridTextColumn.Header> 
        <TextBlock Text="{Binding DataContext.MyProp, 
                       RelativeSource={RelativeSource FindAncestor, 
                       AncestorType={x:Type Window}}}" /> 
    </DataGridTextColumn.Header>
</DataGridTextColumn>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!