如何在DataGridComboBoxColumn中更改单元格值的前景色?

时间:2013-05-17 14:57:02

标签: wpf datagrid

我有一个DataGridComboBoxColumn的网格,我正在尝试更改单元格的前景色(不处于编辑状态)。

我知道我可以使用DataGridTemplateColumn解决此问题,但我想尝试一下。

比较

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

<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>

感谢您的提示!

1 个答案:

答案 0 :(得分:1)

您可以定义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