【问题标题】:Change the Backgroundcolor in a Row if a specific Value is inside the row如果特定值在行内,则更改行中的背景颜色
【发布时间】:2020-01-14 04:02:58
【问题描述】:

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

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

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

我的 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 的“AutoGenereatedColumns”如下: ID、姓名、备注、级别、日期。

我的问题是我不知道要创建触发器,所以它可以对“级别”的值做出反应,因为它是自动生成的。

【问题讨论】:

    标签: c# xaml dataview


    【解决方案1】:

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

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

    类似的东西。

    (未测试但应该可以)

    More info

    【讨论】:

    • 谢谢,但这不起作用。我没有用于绑定的属性“级别”,因为视图的列是自动生成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多