WPF Datagrid-基于对象的数据表自动生成列并设置背景颜色

WPF Datagrid-基于对象的数据表自动生成列并设置背景颜色,wpf,datagrid,Wpf,Datagrid,作为数据处理的一部分,我生成了以下类的DataTable(列计数和行计数各不相同 public class DataGridCell { public string Text { get; set; } public string Background { get; set; } } 我的计划是将DataGrid绑定到此DataTable;每个单元格应显示DataGridCell.Text值,该单元格的背景色应为DataGridCell.background值 我已经厌倦了下面

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

public class DataGridCell
{
    public string Text { get; set; }
    public string Background { get; set; }
}
我的计划是将DataGrid绑定到此DataTable;每个单元格应显示DataGridCell.Text值,该单元格的背景色应为DataGridCell.background值

我已经厌倦了下面的工作

C#

DataTable dtVolume=newdatatable();
for(int i=0;i最小值;p-=0.05)
{
var row=dtVolume.NewRow();
for(int i=0;i
XAML


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

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

XAML


我该怎么做


此处和另一处提供的解决方案不会自动生成列。它们使用DataGridTemplateColumn,但在我的例子中,列需要自动生成,因为列(和行)的数量将发生变化

什么是
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; }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的MyViewModel();
}
}
公共类MyViewModel
{
公共MyViewModel()
{
项目=新列表();
对于(int i=0;i<10;i++)
{
int c=i%8;
添加(新的DataGridCell)
{
Text=$“项#{i}”,
背景=$“{c}{c}{c}{c}{c}{c}{c}”
});
}
}
公共列表项{get;set;}
}
公共类DataGridCell
{
公共字符串文本{get;set;}
公共字符串背景{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>

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


像这样将对象模型与
数据表
混合不是一个好主意。使用简单的标量列值绑定到
IEnumerable
DataTable

dtVolume是DataTable您如何“生成”DataTable?如果你想让任何人都能告诉你你做错了什么,你需要发布这段代码;对于(int i=0;imin;p-=0.05){var row=dtVolume.NewRow();对于(int i=0;i您的方法无效。看看我的答案。
<DataGrid  x:Name="dgVolumes" ItemsSource="{Binding}">
<DataGrid.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Background" Value="{Binding Path=Background}"/>
    </Style>
</DataGrid.CellStyle>
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; }
}
<Grid>
    <DataGrid  x:Name="dgVolumes" ItemsSource="{Binding Items}">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Background" Value="{Binding Background}"/>
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>
</Grid>