Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change foreground color of cell value in DataGridComboBoxColumn?

Tags:

wpf

datagrid

I have a grid with DataGridComboBoxColumn and I am trying to change the foreground color of the cell (not in edit state).

I know that I can workaround this using DataGridTemplateColumn but I'd like to try this.

Compare:

<DataGridComboBoxColumn Header="Is Active" 
  SelectedItemBinding="{Binding IsActive}"
  EditingElementStyle="{StaticResource ComboBoxStyle}"
  ItemsSource="{StaticResource BooleanValues}">
</DataGridComboBoxColumn>

and

<DataGridTemplateColumn Header="IsActive">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding IsActive}" 
        Style="{StaticResource DataGridTextBlockStyle}"/>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
  <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <Grid FocusManager.FocusedElement="{Binding ElementName=combo}">
        <ComboBox Name="combo"               
          SelectedValue="{Binding IsActive}"
          ItemsSource="{StaticResource BooleanValues}" />
       </Grid>
    </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

Thanks for hints!

like image 455
Karel Frajták Avatar asked Jan 22 '26 22:01

Karel Frajták


1 Answers

You can define your DataGridComboBoxColumn CellStyle :

<DataGridComboBoxColumn Header="Is Active" 
                        SelectedItemBinding="{Binding IsActive}"
                        EditingElementStyle="{StaticResource ComboBoxStyle}"
                        ItemsSource="{StaticResource BooleanValues}">
    <DataGridComboBoxColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>

enter image description here

like image 61
Sisyphe Avatar answered Jan 25 '26 10:01

Sisyphe