WPF Datagrid view header binding

前端 未结 2 1955
醉话见心
醉话见心 2020-12-12 07:12

Actually the issue im facing is slight different than what title says. I try to sumarize the issue below.

Class PersonnelViewModel
{

public SelectedPersonne         


        
相关标签:
2条回答
  • 2020-12-12 07:51

    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}"/>
    
    0 讨论(0)
  • 2020-12-12 07:54

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