【问题标题】:WPF Datagrid - Autogenerate columns based on a DataTable of objects and set backgroud colorWPF Datagrid - 根据对象的 DataTable 自动生成列并设置背景颜色
【发布时间】:2023-03-17 15:05:01
【问题描述】:

作为数据处理的一部分,我生成以下类的 DataTable(列数和行数不同)

public class DataGridCell
{
    public string Text { get; set; }
    public string Background { get; set; }
}

我的计划是绑定一个DataGrid到这个DataTable;每个单元格应显示 DataGridCell.Text 值,并且该单元格的背景颜色应为 DataGridCell.Background 值。

我已经厌倦了以下

C#

DataTable dtVolume = new DataTable();
    for (int i = 0; i < ColumnNames.Length; i++)
    {
        dtVolume.Columns.Add(ColumnNames[i]);
    }
    for (double p = max; p > min; p -= 0.05)
    {
        var row = dtVolume.NewRow();
        for (int i = 0; i < ColumnNames.Length; i++)
        {
            row[i] = new DataGridCell
            {
                Text = i,
                Background = i % 2 == 0 ? "LightGray" : "Red"
            };
        }
        dtVolume.Rows.Add(row);
    }

dgVolumes.DataContext = dtVolume.DefaultView;

XAML

<DataGrid  x:Name="dgVolumes" ItemsSource="{Binding}">
<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Background" Value="LightGray"/>
    </Style>
</DataGrid.CellStyle>

这给了我一个 DataGrid,单元格背景设置为 LightGray,但显示的文本是 Namespace.DataGridCell

下面的 XAML 出错,因为 {Binding Path=Background} 失败,因为上下文是 DataRowView

XAML

<DataGrid  x:Name="dgVolumes" ItemsSource="{Binding}">
<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Background" Value="{Binding Path=Background}"/>
    </Style>
</DataGrid.CellStyle>

我该怎么做?

WPF Binding to a DataGrid from a DataTable of ObjectsChange DataGrid cell colour based on values 提供的解决方案不会自动生成列。他们使用 DataGridTemplateColumn 但在我的情况下,列需要自动生成,因为列(和行)的数量会发生变化。

【问题讨论】:

  • 您究竟是如何“生成”数据表的?如果您希望任何人能够告诉您您做错了什么,您需要发布此代码。
  • 类似于DataTable dtVolume = new DataTable(); for (int i = 0; i &lt; ColumnNames.Length; i++) { dtVolume.Columns.Add(ColumnNames[i]); } for (double p = max; p &gt; min; p -= 0.05) { var row = dtVolume.NewRow(); for (int i = 0; i &lt; ColumnNames.Length; i++) { row[i] = new DataGridCell{ Text = i, Background = i % 2 == 0 ? "LightGray" : "Red" } } dtVolume.Rows.Add(row); }的东西
  • 你的方法行不通。看我的回答。

标签: wpf datagrid


【解决方案1】:

dtVolume.DefaultView 是什么?

使用标准绑定可以正常工作,所以我想知道您尝试将 DataGrid 绑定到哪种对象。

这是我用过的代码,让我知道你这边有什么不同。

ViewModel 和 cs 代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new MyViewModel();
    }
}

public class MyViewModel
{
    public MyViewModel()
    {
        Items = new List<DataGridCell>();
        for (int i = 0; i < 10; i++)
        {
            int c = i % 8;
            Items.Add(new DataGridCell
            {
                Text = $"Item #{i}",
                Background = $"#{c}{c}{c}{c}{c}{c}"
            });
        }
    }

    public List<DataGridCell> Items { get; set; }
}

public class DataGridCell
{
    public string Text { get; set; }
    public string Background { get; set; }
}

XAML 代码:

<Grid>
    <DataGrid  x:Name="dgVolumes" ItemsSource="{Binding Items}">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Background" Value="{Binding Background}"/>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
</Grid>

【讨论】:

  • dtVolume 是一个数据表
【解决方案2】:

您不能真正将DataGridCell 对象存储在DataRow 中。有关此问题的更多信息,请参阅我的回答:

Binding an object to data grid cell - conditional cell style

像这样将对象模型与DataTable 混合不是一个好主意。使用简单的标量列值绑定到任一 IEnumerable&lt;DataGridCell&gt; DataTable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 2019-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多