C# 如果行内有特定值,请更改行中的背景色

C# 如果行内有特定值,请更改行中的背景色,c#,xaml,dataview,C#,Xaml,Dataview,我在更改数据网格中的行的颜色时遇到了一些麻烦 我的Datagrid有一个DataView作为源。DataView从SQL查询中获取其值。(有多个SQL查询,因此DataView及其列的内容不同) 现在我的问题是,如果DataView中的值是特定字符串,我必须更改行的背景色。例如:如果字符串的值为“Info”,则背景颜色应为“蓝色”;如果值为“Error”,则背景颜色应为红色 我的DataGrid如下所示: <DataGrid ItemsSource="{Binding GetDataVie

我在更改数据网格中的行的颜色时遇到了一些麻烦

我的Datagrid有一个DataView作为源。DataView从SQL查询中获取其值。(有多个SQL查询,因此DataView及其列的内容不同)

现在我的问题是,如果DataView中的值是特定字符串,我必须更改行的背景色。例如:如果字符串的值为“Info”,则背景颜色应为“蓝色”;如果值为“Error”,则背景颜色应为红色

我的DataGrid如下所示:

<DataGrid ItemsSource="{Binding GetDataView}"
                      Foreground="White"
                      Style="{DynamicResource DataGridStyle2}"
                      RowHeaderWidth="0"
                      BorderThickness="1"
                      HorizontalGridLinesBrush="#FF9A969E"
                      VerticalGridLinesBrush="#FF9A969E"
                      RowBackground="{x:Null}"
                      HorizontalAlignment="Stretch"
                      Margin="10,0,10,30"
                      Grid.Row="3"
                      VerticalAlignment="Stretch">
            </DataGrid>

DataView的“AutoGeneratedColumn”如下所示: ID、名称、注释、级别、日期


我的问题是,我不知道如何创建触发器,因此它可以对“Level”的值作出反应,因为它是自动生成的。

您需要一个用于行样式的数据触发器

<DataGrid Name="dataGrid1" Margin="12,12,0,0">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Level}" Value="somestring">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

像这样的

(未测试,但应能正常工作)


行样式需要数据触发器

<DataGrid Name="dataGrid1" Margin="12,12,0,0">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="LightBlue" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Level}" Value="somestring">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

像这样的

(未测试,但应能正常工作)


谢谢,但这不起作用。我没有用于绑定的属性“Level”,因为视图的列是自动生成的。谢谢,但这不起作用。我没有用于绑定的属性“Level”,因为视图的列是自动生成的。