如何使WPF DataGrid列标题透明?

时间:2022-04-07 19:43:49

I am trying to make the column header of my WPF Datagrid to be transparent.

我试图使WPF数据网格的列标题透明。

I am able to set it to a color without problem, but I can't have it transparent. Here is what I tried:

我可以把它设置成一个没有问题的颜色,但是我不能让它透明。以下是我的尝试:

<Style x:Key="DatagridColumnHeaderStyle" TargetType="{x:Type tk:DataGridColumnHeader}">
   <Setter Property="Background" Value="Transparent" />
   <Setter Property="Foreground" Value="#C2C4C6" />
</Style>

<Style x:Key="DashboardGridStyle" TargetType="{x:Type tk:DataGrid}">
   <Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderStyle}" />
   <Setter Property="Background" Value="Transparent" />
   <Setter Property="RowBackground" Value="Transparent" />
</Style>

<tk:DataGrid Style="{StaticResource DashboardGridStyle}" >
...
</tk:DataGrid>

With this code, it seems to take the default brush.

使用这段代码,它似乎采用了默认的笔刷。

What am I missing?

我缺少什么?

2 个解决方案

#1


4  

I used Snoop to take a look at what was happening. It seems that another DataGridColumnHeader is always created behind the one you can modify, and it's not affected by changes on styles. When you set a transparent background, in fact is being correctly applied, so what you see is that ghost header behind (which has the usual grey background).

我用窥探来观察发生了什么事。看起来另一个DataGridColumnHeader总是在您可以修改的那个后面创建的,它不受样式更改的影响。当你设置一个透明的背景时,实际上是被正确应用的,所以你看到的是后面的那个鬼头(它有通常的灰色背景)。

If you apply a coloured background and play with Opacity, you will see how the two colours are mixed. I don't know if this can be solved.

如果您应用一个彩色背景和发挥不透明度,您将看到如何两个颜色混合。我不知道这个能不能解决。

#2


4  

With the answer from Natxo (thanks!), I was able to find a solution. And it is a simple one too!

有了Natxo的答案(谢谢!),我找到了一个解决方案。这也很简单!

Knowing that there was another DataGridColumnHeader behind the one we can modify through the ColumnHeaderStyle, I just had to set a style that will affect all DataGridColumnHeader:

知道在我们可以通过ColumnHeaderStyle修改的那个后面还有另一个DataGridColumnHeader,我只需要设置一个样式就可以影响所有DataGridColumnHeader:

<Style TargetType="{x:Type tk:DataGridColumnHeader}">
   <Setter Property="Background" Value="Transparent" />
</Style>

#1


4  

I used Snoop to take a look at what was happening. It seems that another DataGridColumnHeader is always created behind the one you can modify, and it's not affected by changes on styles. When you set a transparent background, in fact is being correctly applied, so what you see is that ghost header behind (which has the usual grey background).

我用窥探来观察发生了什么事。看起来另一个DataGridColumnHeader总是在您可以修改的那个后面创建的,它不受样式更改的影响。当你设置一个透明的背景时,实际上是被正确应用的,所以你看到的是后面的那个鬼头(它有通常的灰色背景)。

If you apply a coloured background and play with Opacity, you will see how the two colours are mixed. I don't know if this can be solved.

如果您应用一个彩色背景和发挥不透明度,您将看到如何两个颜色混合。我不知道这个能不能解决。

#2


4  

With the answer from Natxo (thanks!), I was able to find a solution. And it is a simple one too!

有了Natxo的答案(谢谢!),我找到了一个解决方案。这也很简单!

Knowing that there was another DataGridColumnHeader behind the one we can modify through the ColumnHeaderStyle, I just had to set a style that will affect all DataGridColumnHeader:

知道在我们可以通过ColumnHeaderStyle修改的那个后面还有另一个DataGridColumnHeader,我只需要设置一个样式就可以影响所有DataGridColumnHeader:

<Style TargetType="{x:Type tk:DataGridColumnHeader}">
   <Setter Property="Background" Value="Transparent" />
</Style>