Paula Kwakernaak
Paula Kwakernaak

Reputation: 13

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.

Upvotes: 1

Views: 45

Answers (2)

mm8
mm8

Reputation: 169420

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>

Upvotes: 0

JUNAID MUSLA
JUNAID MUSLA

Reputation: 134

Since IsReadOnly property of DataGridTextColumn is set in xaml itself, you can check below code for your reference:

Style

    <Style x:Key="DataGridColumnHeaderStyle" TargetType = "DataGridColumnHeader">
    <Setter Property="Background" Value = "DarkBlue"/>
    <Setter Property="BorderBrush" Value = "Black"/>
    <Setter Property="BorderThickness" Value = "0.5"/>
    <Setter Property="HorizontalContentAlignment" Value = "Center"/>
    </Style>
    <Style x:Key="DataGridColumnHeaderReadOnlyStyle" BasedOn="{StaticResource DataGridColumnHeaderStyle}" TargetType ="DataGridColumnHeader">
    <Setter Property="Background" Value = "red"/>
    </Style>

Giving style to control

 <DataGrid Margin="{StaticResource ControlThickness}"  SelectionUnit = "FullRow">
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly = "True" HeaderStyle="{StaticResource DataGridColumnHeaderReadOnlyStyle}" Header="H1" Width="40"/>
        <DataGridTextColumn IsReadOnly = "False" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Header="H2"  Width="40"/>
        <DataGridTextColumn IsReadOnly = "True"  HeaderStyle="{StaticResource DataGridColumnHeaderReadOnlyStyle}" Header="H3" Width="40"/>
        <DataGridTextColumn IsReadOnly = "False" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Header="H4" Width="40"/>
    </DataGrid.Columns>
</DataGrid>

Upvotes: 1

Related Questions