Binding DataGrid column Header to DataContext

前端 未结 2 1920
离开以前
离开以前 2020-12-01 13:18

I know this question has been asked before, but none of the \"previous\" answers seems to work for me...

I have implemented a functionality for multi languages in my

相关标签:
2条回答
  • 2020-12-01 13:48

    correct. there is no elemet in visual tree directly mapping to DataGridTextColumn so you can't use RelativeSource with AncestorType (i.e. DataGridTextColumn is not a control hence it doesn't have a parent control). below code should work fine

    <DataGridTextColumn Binding="{Binding Name}">
      <DataGridTextColumn.Header>
        <TextBlock Text="{Binding DataContext.Text[Name],
                          RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
      </DataGridTextColumn.Header>
    </DataGridTextColumn>
    
    0 讨论(0)
  • 2020-12-01 13:56

    You may need to walk back up the tree to get the DataContext you want:

    <DataGridTextColumn Binding="{Binding Name}">
        <DataGridTextColumn.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding DataContext.Text[Name],
                           RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
            </DataTemplate>
        </DataGridTextColumn.HeaderTemplate>
    </DataGridTextColumn>
    

    Doing this directly on the Header property will not work because it cannot be resolved in-place as the column is an abstract object not appearing in the tree.

    0 讨论(0)
提交回复
热议问题