Datagrid foreground colour not working

眉间皱痕 提交于 2019-12-02 12:47:44

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!