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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21764911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:50:05  来源:igfitidea点击:

How to change DataGrid cell background color based on cell value

c#wpfdatagriddatatable

提问by Paulo Rocha

I'm trying to change the Backgroundcolor of my DataGridcells that contains the word 'Modify'.

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

In WinFormsI've accomplished that with this code:

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

I populate this DataGridwith a DataTable.

DataGrid用一个DataTable.

I know very little about WPFyet, so if you have any suggestions, please be very specific.

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

采纳答案by Paulo Rocha

I did the following to fix my issue:

我做了以下事情来解决我的问题:

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