C# Datagrid行前景色更改不起作用
这是以下内容的延续: 因此,我通过以下方式构建数据网格:C# Datagrid行前景色更改不起作用,c#,wpf,datagrid,row,foreground,C#,Wpf,Datagrid,Row,Foreground,这是以下内容的延续: 因此,我通过以下方式构建数据网格: string[] columnLabels = new string[] { "Column 0", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" }; foreach (string label in columnLabels) { DataGridTextColumn column = new DataGridTextColumn(); column.
string[] columnLabels = new string[] { "Column 0", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };
foreach (string label in columnLabels)
{
DataGridTextColumn column = new DataGridTextColumn();
column.Header = label;
column.Binding = new Binding(label.Replace(' ', '_'));
dtgResults.Columns.Add(column);
}
int[] ivalues = new int[] { 0, 1, 2, 3 };
string[] svalues = new string[] { "A", "B", "C", "D" };
dynamic row = new ExpandoObject();
for (int i = 0; i < 6; i++)
{
switch (i)
{
case 0:
case 1:
case 2:
string str = columnLabels[i].Replace(' ', '_');
((IDictionary<String, Object>)row)[str] = ivalues[i];
break;
case 3:
case 4:
case 5:
string str2 = columnLabels[i].Replace(' ', '_');
((IDictionary<String, Object>)row)[str2] = svalues[i - 3];
break;
}
}
dtgResults.Items.Add(row);
但这不会改变你的想法,把它添加到你的xaml中
<DataGrid Name="dtg" Background="Transparent">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="DataGridCell.Loaded" Handler="DataGridCell_Load"/>
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGrid.Resources>
它为我改变了文本的颜色。好的,通过这样做,我也得到了单元格内容和行,但是列呢?反过来说:通过int column=cell.column.DisplayIndex得到列;按int行的行=-1;DataGridRow rowContainer=parentDataGrid.GetRow(0);对于(int iii=0;iii
<DataGrid Name="dtg" Background="Transparent">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="DataGridCell.Loaded" Handler="DataGridCell_Load"/>
<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGrid.Resources>
private void DataGridCell_Load(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
cell.Foreground = new SolidColorBrush(Colors.Red);
}