wpf 更改 GridViewColumn 标题颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30505385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:09:49  来源:igfitidea点击:

Change GridViewColumn header color

wpflistview

提问by roz wer

I have this ListView:

我有这个列表视图:

<ListView  Name="lvFiles" Background="Transparent" BorderThickness="0">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="File name" />
            <GridViewColumn Header="Size" Width="50" />
        </GridView>
    </ListView.View>
</ListView>

How can i set the Header color ?

如何设置标题颜色?

回答by Xavier

I have not used GridViewpersonally, but a quick web search led me to:

GridView个人没有使用过,但快速的网络搜索使我:

GridView Column Header Styles and Templates Overview

GridView 列标题样式和模板概述

That page links out to other pages to explain various things you can do with GridViewcolumn headers. It looks like there are multiple ways to set the "color" (not sure if you mean Foregroundor Background), but using a Styleseems like what you probably want. You can set a style to apply to all column headers like this:

该页面链接到其他页面以解释您可以使用GridView列标题执行的各种操作。看起来有多种方法可以设置“颜色”(不确定您的意思是Foreground还是Background),但使用 aStyle似乎是您可能想要的。您可以设置样式以应用于所有列标题,如下所示:

<GridView>
    <GridView.ColumnHeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <!-- Set any properties you want to set -->
            <Setter Property="Background" Value="LightBlue" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </GridView.ColumnHeaderContainerStyle>
</GridView>

You can override the header style for a specific column like this:

您可以像这样覆盖特定列的标题样式:

<GridViewColumn Header="Something">
    <GridViewColumn.HeaderContainerStyle>
        <Style TargetType="{x:Type GridViewColumnHeader}">
            <!-- Set any properties you want to set -->
            <Setter Property="Background" Value="DarkBlue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </GridViewColumn.HeaderContainerStyle>
</GridViewColumn>

Edit: While you can customize most thing using styes and data templates (which are covered in the overview I linked at the top of this answer), there are certain things that are not customizable because they are baked into the control template – such as the border that appears above and below each column header. If you need to modify any of those things, then you will need to setup a style that sets the Templateproperty to a custom ControlTemplatethat you create.

编辑:虽然您可以使用 stye 和数据模板自定义大多数内容(在我在此答案顶部链接的概述中进行了介绍),但有些内容是不可自定义的,因为它们已融入控制模板中——例如出现在每个列标题上方和下方的边框。如果您需要修改任何这些内容,那么您将需要设置一个样式,将Template属性设置为ControlTemplate您创建的自定义。

If you want to make your own control template for a column header, I suggest you start by copying the example templateinto your application resources. Then tweak the parts you need to tweak. Keep in mind that setting the Templateproperty on a control completely replaces the template, meaning you have to define the entire template that you want to use, not simply define parts that you want to be different from the default.

如果您想为列标题制作自己的控件模板,我建议您首先将示例模板复制到您的应用程序资源中。然后调整您需要调整的部分。请记住,Template在控件上设置属性会完全替换模板,这意味着您必须定义要使用的整个模板,而不是简单地定义要与默认值不同的部分。