如何为DataGrid wpf的特定单元格设置背景?

时间:2017-11-01 18:31:42

标签: c# wpf xaml datagrid

我有一个DataTable,我希望更改特定单元格的背景,使其'place'字段为真。

我在C#代码中设置了DataGrid的ItemSource。

我尝试使用此代码但不起作用:

 <DataGrid x:Name="dg">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Place}"
                                 Value="True">
                        <Setter Property="Background" Value="Yellow"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

在C#代码中:

DataTable dt = new DataTable();
            dt.Columns.Add("Time", typeof(string));
            dt.Columns.Add("Place", typeof(string));
            dt.Rows.Add( "23:00", "true");
            dt.Rows.Add( "21:00", "true");
            dt.Rows.Add( "19:00", "false");
            dg.ItemsSource = dt.AsDataView();

如何将DataTrigger绑定到我提交的DataTable“位置”?

1 个答案:

答案 0 :(得分:0)

这个问题的答案在本说明中:

字符串,因为值区分大小写

字段中的字符串必须与tag中的字符串相等。