Datagrid前景色不工作 [英] Datagrid foreground colour not working

查看:122
本文介绍了Datagrid前景色不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的datagrid显示二维数据。
我在测试项目中尝试过,结果很好。





这是xmal:

 < Grid> 
< DataGrid Name =dgMargin =50FontSize =26CellEditEnding =dg_CellEditEndingBeginningEdit =dg_BeginningEditLoadingRow =DataGrid_LoadingRowEnableRowVirtualization =FalseAutoGeneratingColumn =dg_AutoGeneratingColumn/>
< / Grid>

和相关事件代码:

  private void DataGrid_LoadingRow(object sender,System.Windows.Controls.DataGridRowEventArgs e)
{
e.Row.Header =R+((e.Row。 GetIndex())+ 1).ToString();
}

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();

}

那么我必须把它放在我真正的项目中一个样式的窗口。



所以我把相同的非常容易的xaml(添加背景和前景在这里我有一个渐变背景),以便xamls是:

 < Grid> 
< DataGrid Name =dtgNestsMargin =50FontSize =26Background =WhiteForeground =BlackHeadersVisibility =AllCellEditEnding =dg_CellEditEndingBeginningEdit =dg_BeginningEditLoadingRow = DataGrid_LoadingRowEnableRowVirtualization =FalseAutoGeneratingColumn =dg_AutoGeneratingColumn/>
< / Grid>

,效果是:



a href =https://i.stack.imgur.com/y6heg.png =nofollow noreferrer>



所以简单地说,没有在任何部分设置FOREGROUND。那就是:




  • 单元格

  • 行标题

  • 列标题



所以没有地方。
任何人都可以告诉我为什么?
提前谢谢
Patrick

解决方案

在WPF DataGrid中,所有与cell相关的设计需要被设置为列的 ElementStyle ,它们覆盖了Grid中设置的前景。尝试以下内容:



在您的XAML资源中

 code>< Style x:Key =BlackCellStyleTargetType ={x:Type TextBlock}> 
< Setter Property =ForegroundValue =Black/>
< / Style>

在您的 AutoGeneratingColumn 处理程序中:

  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)作为样式;
}

这将直接将前景应用到您的单元格


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:

<Grid >
<DataGrid Name="dg" Margin="50" FontSize="26" CellEditEnding="dg_CellEditEnding" BeginningEdit="dg_BeginningEdit"  LoadingRow="DataGrid_LoadingRow" EnableRowVirtualization="False"  AutoGeneratingColumn="dg_AutoGeneratingColumn"/>
</Grid>

and the relevant event code:

 private void DataGrid_LoadingRow(object sender, System.Windows.Controls.DataGridRowEventArgs e)
{
  e.Row.Header = "R" + ((e.Row.GetIndex()) + 1).ToString();
}

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();

}

then I have to put it in my real project which is a styled window.

So I put the same very easy xaml (adding background and foreground fore here I have a gradient background) so that the xamls is:

<Grid >
<DataGrid Name="dtgNests" Margin="50" FontSize="26" Background="White" Foreground="Black" HeadersVisibility="All" CellEditEnding=" dg_CellEditEnding" BeginningEdit="dg_BeginningEdit"  LoadingRow="DataGrid_LoadingRow" EnableRowVirtualization="False"  AutoGeneratingColumn="dg_AutoGeneratingColumn"/>
</Grid>

and the effect is:

so in short the FOREGROUND is not being set in any part. That is:

  • cells
  • row headers
  • column headers

so nowhere. Can anyone tell me why? thank you in advance Patrick

解决方案

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

这篇关于Datagrid前景色不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆