Datagrid foreground colour not working

前端 未结 1 1497
时光说笑
时光说笑 2021-01-26 15:07

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:



        
相关标签:
1条回答
  • 2021-01-26 15:25

    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

    0 讨论(0)
提交回复
热议问题