无法更改 DataGrid Winui3 中的 DataGridColumnHeader 背景颜色。

huangapple go评论91阅读模式
英文:

Unable to change background color of DataGridColumnHeader in DataGrid Winui3

问题

I am creating a WinUI3 project and using Datagrid from

CommunityToolkit.WinUI.UI.Controls.DataGrid

I am unable to change DataGridColumnHeader using Setter while Foreground changes perfectly fine. My Code :

<controls:DataGrid.ColumnHeaderStyle>
   <Style TargetType="primitives:DataGridColumnHeader">
   <Setter Property="FontSize" Value="10"/>
   <Setter Property="FontWeight" Value="Bold"/>
   <Setter Property="HorizontalAlignment" Value="Stretch"/>
   <Setter Property="Background" Value="Green"/>       //This doesnt work                        
   <Setter Property="Foreground" Value="Red"/> //This works
                     
   </Style>
                           
</controls:DataGrid.ColumnHeaderStyle>

Output

无法更改 DataGrid Winui3 中的 DataGridColumnHeader 背景颜色。

No matter what I do, the header background remains white. What should I do to change Column Header Background color?

Edit

I saw an issue on Github about it, but didn't make sense to me how to implement it !

https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3393

英文:

I am creating a WinUI3 project and using Datagrid from

CommunityToolkit.WinUI.UI.Controls.DataGrid

I am unable to change DataGridColumnHeader using Setter while Foreground changes perfectly fine. My Code :

 &lt;controls:DataGrid.ColumnHeaderStyle&gt;
   &lt;Style TargetType=&quot;primitives:DataGridColumnHeader&quot;&gt;
   &lt;Setter Property=&quot;FontSize&quot; Value=&quot;10&quot;/&gt;
   &lt;Setter Property=&quot;FontWeight&quot; Value=&quot;Bold&quot;/&gt;
   &lt;Setter Property=&quot;HorizontalAlignment&quot; Value=&quot;Stretch&quot;/&gt;
   &lt;Setter Property=&quot;Background&quot; Value=&quot;Green&quot;/&gt;       //This doesnt work                        
   &lt;Setter Property=&quot;Foreground&quot; Value=&quot;Red&quot;/&gt; //This works
                     
   &lt;/Style&gt;
                           
&lt;/controls:DataGrid.ColumnHeaderStyle&gt;

Output

无法更改 DataGrid Winui3 中的 DataGridColumnHeader 背景颜色。

No matter what I do, the header background remains white. What should I do to change Column Header Background color?

Edit

I saw an issue on Github about it, but didn't make sense to me how to implement it !

https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3393

答案1

得分: 1

你需要覆盖这3种颜色:

&lt;Page.Resources&gt;
    &lt;Color x:Key=&quot;GreenishColor&quot;&gt;#FF60B560&lt;/Color&gt;
    &lt;Color x:Key=&quot;YellowishColor&quot;&gt;#FFFFF700&lt;/Color&gt;
    &lt;Color x:Key=&quot;BlueishColor&quot;&gt;#FF00e5ff&lt;/Color&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderBackgroundColor&quot; ResourceKey=&quot;GreenishColor&quot;/&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderHoveredBackgroundColor&quot; ResourceKey=&quot;YellowishColor&quot;/&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderPressedBackgroundColor&quot; ResourceKey=&quot;BlueishColor&quot;/&gt;
&lt;/Page.Resources&gt;

你也可以在你的DataGrid内部这样做:

&lt;controls:DataGrid&gt;
    &lt;controls:DataGrid.Resources&gt;
        &lt;Color x:Key=&quot;GreenishColor&quot;&gt;#FF60B560&lt;/Color&gt;
        &lt;Color x:Key=&quot;YellowishColor&quot;&gt;#FFFFF700&lt;/Color&gt;
        &lt;Color x:Key=&quot;BlueishColor&quot;&gt;#FF00e5ff&lt;/Color&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderBackgroundColor&quot; ResourceKey=&quot;GreenishColor&quot;/&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderHoveredBackgroundColor&quot; ResourceKey=&quot;YellowishColor&quot;/&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderPressedBackgroundColor&quot; ResourceKey=&quot;BlueishColor&quot;/&gt;
    &lt;/controls:DataGrid.Resources&gt;
&lt;/controls:DataGrid&gt;
英文:

You need to override these 3 colors:

&lt;Page.Resources&gt;
    &lt;Color x:Key=&quot;GreenishColor&quot;&gt;#FF60B560&lt;/Color&gt;
    &lt;Color x:Key=&quot;YellowishColor&quot;&gt;#FFFFF700&lt;/Color&gt;
    &lt;Color x:Key=&quot;BlueishColor&quot;&gt;#FF00e5ff&lt;/Color&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderBackgroundColor&quot; ResourceKey=&quot;GreenishColor&quot;/&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderHoveredBackgroundColor&quot; ResourceKey=&quot;YellowishColor&quot;/&gt;
    &lt;StaticResource x:Key=&quot;DataGridColumnHeaderPressedBackgroundColor&quot; ResourceKey=&quot;BlueishColor&quot;/&gt;
&lt;/Page.Resources&gt;

you can also do this inside your DataGrid like this:

&lt;controls:DataGrid&gt;
    &lt;controls:DataGrid.Resources&gt;
        &lt;Color x:Key=&quot;GreenishColor&quot;&gt;#FF60B560&lt;/Color&gt;
        &lt;Color x:Key=&quot;YellowishColor&quot;&gt;#FFFFF700&lt;/Color&gt;
        &lt;Color x:Key=&quot;BlueishColor&quot;&gt;#FF00e5ff&lt;/Color&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderBackgroundColor&quot; ResourceKey=&quot;GreenishColor&quot;/&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderHoveredBackgroundColor&quot; ResourceKey=&quot;YellowishColor&quot;/&gt;
        &lt;StaticResource x:Key=&quot;DataGridColumnHeaderPressedBackgroundColor&quot; ResourceKey=&quot;BlueishColor&quot;/&gt;
    &lt;/controls:DataGrid.Resources&gt;
&lt;/controls:DataGrid&gt;

huangapple
  • 本文由 发表于 2023年4月6日 19:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定