如何根据单元格值更改DataGrid单元格背景颜色

时间:2014-02-13 20:36:05

标签: c# wpf datagrid datatable

我正在尝试更改包含“修改”一词的Background个单元格的DataGrid颜色。

WinForms我用这段代码完成了这个:

DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.DarkCyan;

foreach (DataGridViewRow Row in dataGridView1.Rows)
{
    if (Row.Cells["Permission"].Value.ToString().Contains("Modify"))
    {
        Row.Cells["Permission"].Style = style;
    }
}

我使用DataGrid填充此DataTable

我对WPF知之甚少,所以如果您有任何建议,请非常具体。

1 个答案:

答案 0 :(得分:1)

我做了以下修复我的问题:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Height="173" Margin="53,127,0,0" VerticalAlignment="Top" Width="378" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Binding="{Binding Selecione}" Header="Selecione"/>
                <DataGridTextColumn Binding="{Binding Grupos}" Header="Grupos"/>
                <DataGridTextColumn Binding="{Binding Permissoes}" Header="Permissões">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Permissoes}" Value="Modify">
                                    <Setter Property="Background" Value="Green"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>`enter code here`
            </DataGrid.Columns>
        </DataGrid>