【问题标题】:how to set background for specific cells of DataGrid wpf?如何为 DataGrid wpf 的特定单元格设置背景?
【发布时间】:2017-11-01 18:31:41
【问题描述】:

我有一个 DataTable,我想更改其“位置”字段为 true 的特定单元格的背景。

我在 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 的“位置”绑定?

【问题讨论】:

  • 请在此演示文稿后面展示您的模型的一些代码。
  • 很难理解你的意思。我认为您需要添加一些代码才能在此处获得正确的帮助。
  • 我编辑我的问题并添加 C# 代码。请看。
  • 所以Binding Place 应该可以解决问题。另请注意字符串,因为值区分大小写。

标签: c# wpf xaml datagrid


【解决方案1】:

这个问题的答案在这个笔记里:

字符串作为值区分大小写

字段位置的字符串必须与标签值的字符串相等。

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 2018-04-14
    • 2011-12-12
    • 1970-01-01
    • 2017-10-04
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多