wpfxamldatatriggerdatagridtextcolumn

How to set Backgroundcolor of the DatagridTextColumn.Header based on the property IsReadOnly of the DatagridTextColumn


I am working with WPF (C#, MVVM) with a DataGridTextColumn. I want to style the background of the Header of each Column based on its IsReadOnly property. I want to use a DataTrigger, but I don't know how to set the source property. I can give each DataGridTextColumn Header its own style, but I want one style for all Textcolumns.
As the user can edit the content of a Column (IsReadOnly = "False") than the background of the ColumnHeader has to be set to Dark Blue.

<Style x:Key="EditableHeaderStyle" TargetType = "DataGridColumnHeader">
  <Setter Property="Background" Value = "Light Blue"/>
  <Style.Triggers>
    <DataTrigger Binding="?**Source**... , Path= IsReadOnly" Value="True">
      <Setter Property="Bacground" Value="Purple"/>
    </DataTrigger>
    <DataTrigger Binding="?**Source**...., Path=IsReadOnly" Value="False">
      <Sette Property="Background" Value="Dark Blue"/>
    </DataTrigger>
   </Style.Triggers>
 </Style>       

<DataGrid 
    Selectionunit = "FullRow"
    ItemSource = "{Binding Persons}">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Name}" IsReadOnly = "True"/>
  </DataGridClumns>
</DataGrid>

I have tried to set the Binding source to ElementName, that works, but still on one DataGridTextColumn. I want to set this to all the DataGridTextColumns within the DataGrid. The styling as a HeaderStyle to use it on any DataGrid in my application.

Please can someone explain how this can be done.


Solution

  • Bind to the IsReadOnly property of the Column property of the DataGridColumnHeader element itself like this:

    <DataTrigger Binding="{Binding Column.IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
        <Setter Property="Background" Value="Purple"/>
    </DataTrigger>