Introduction: Due to business needs for WPF in the DataGrid control the number of rows in statistics , but the statistics of the number of rows displayed on the column heading .
If we use conventional Binding means updating DataGridTextColunm the Header word is no effect , because the WPF visual tree in no DataGridTextColumn element to map it (DataGridTextColumn not control, so it has no parent control) .
Error demonstration:
<DataGridTextColumn Binding="{Binding Test}" Header="{Binding ColName}"/>
Because the visual tree mechanism, we just need column headings as a control can be (Note: Header property will fail, need to initialize the Binding field) :
<DataGridTextColumn Binding="{Binding Test}"Header="列名"> <DataGridTextColumn.HeaderTemplate> <DataTemplate> <TextBlock Text="{Binding DataContext.ColName,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay, RelativeSource={RelativeSource AncestorType=DataGrid}}"/> </DataTemplate> </DataGridTextColumn.HeaderTemplate> </DataGridTextColumn>
I Binding field name is ColName , it replaced into your field name.