I have a simple datagrid that display bidimensional data. I have tried it in a test project and the result is nice.
Here is the xmal:
In a WPF DataGrid, all cell-related design needs to be set as the Column's ElementStyle
, which overrides the foreground set in your Grid. Try the following:
In your XAML resources:
<Style x:Key="BlackCellStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black" />
</Style>
In your AutoGeneratingColumn
handler:
private void dg_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
{
string str = e.PropertyName;
int num = int.Parse(e.PropertyName);
e.Column.Header = "C" + (num + 1).ToString();
e.Column.ElementStyle = FindResource("BlackCellStyle") as Style;
}
This will apply the foreground directly to your cells