如何在 WPF Datagrid 上格式化标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/506921/
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-08 20:00:56  来源:igfitidea点击:

How do I format headers on a WPF Datagrid?

wpfdatagrid

提问by Edward Tanguay

I am using the WPF Datagrid from Codeplex.

我正在使用 Codeplex 的 WPF Datagrid。

I am able to style the rows and with the following attributes in the dg:DataGrid element.

我能够在 dg:DataGrid 元素中设置行的样式并使用以下属性。

But how do I style the Headers? I find 100s of examples on the web which define Styles and use e.g. x:Key="DataGridColumnHeaderStyle" in the Datagrid element, but none of them seem to work for me.

但是我如何设置标题的样式?我在网上找到了 100 多个定义样式并在 Datagrid 元素中使用例如 x:Key="DataGridColumnHeaderStyle" 的示例,但它们似乎都不适合我。

How can I just e.g. change the Datagrid Header background to orange on this DataGrid?

我怎样才能在这个 DataGrid 上将 Datagrid Header 背景更改为橙色?

<dg:DataGrid AlternatingRowBackground="#ddd" 
    RowBackground="#eee" 
    Name="theGrid1" 
    VerticalAlignment="Stretch" 
    AutoGenerateColumns="False" 
    BorderBrush="#ddd">
...
</dg:DataGrid>

采纳答案by user61477

The style in this case is in a file called generic.xaml it should be loacted in a themems folder in your project.

在这种情况下,样式位于名为 generic.xaml 的文件中,它应该位于项目中的 themems 文件夹中。

find it and open it. inside you will find this line that controls the background of the column headers

找到它并打开它。在里面你会发现这一行控制列标题的背景

 <dg:DataGridHeaderBorder SortDirection="{TemplateBinding SortDirection}"
                                     IsHovered="{TemplateBinding IsMouseOver}"
                                     IsPressed="{TemplateBinding IsPressed}"
                                     IsClickable="{TemplateBinding CanUserSort}"
                                     Background="{TemplateBinding Background}"
                                     BorderBrush="{TemplateBinding BorderBrush}"
                                     BorderThickness="{TemplateBinding BorderThickness}"
                                     Padding ="{TemplateBinding Padding}"
                                     SeparatorVisibility="{TemplateBinding SeparatorVisibility}"


                         SeparatorBrush="{TemplateBinding SeparatorBrush}">

basically its defined at another place in the template: this will explain TemlateBinding to you MSDN TemplateBinding

基本上它是在模板中的另一个地方定义的:这将向您解释 TemplateBinding MSDN TemplateBinding

HTH, Eric

HTH,埃里克

回答by skybluecodeflier

There is also a property on the DataGrid that allows for styling the header:

DataGrid 上还有一个属性允许设置标题样式:

<DataGrid.ColumnHeaderStyle>
     <Style TargetType="{x:Type DataGridColumnHeader}">
          <Setter Property="FontWeight"
                  Value="Bold" />
     </Style>
</DataGrid.ColumnHeaderStyle>

回答by Developer

Here is another sample

这是另一个示例

  <DataGrid AutoGenerateColumns="False" Height="200" 
                  HorizontalAlignment="Left" Name="dgDownloads" 
                  VerticalAlignment="Top" Width="777"  
                  Background="Black" RowBackground="Gray"  Foreground="White"
                  AlternatingRowBackground="Gray" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
            <DataGrid.Columns>

                <DataGridTextColumn 
                Header="{lex:LocTextExtension Key=Name, Dict=Resources, Assembly=PreShow.Player}"
                Width="220"
                IsReadOnly="True"
                Binding="{Binding Filename}" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap"/>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                    <DataGridTextColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"  Value="Bold" />
                            <Setter Property="Foreground"  Value="Yellow" />
                            <Setter Property="Background"  Value="Black" />
                        </Style>
                    </DataGridTextColumn.HeaderStyle>
                </DataGridTextColumn>

                <DataGridCheckBoxColumn 
                  IsReadOnly="True"
                  Header="{lex:LocTextExtension Key=Success, Dict=Resources, Assembly=PreShow.Player}" 
                  Width="60"
                  Binding="{Binding IsSuccess}"
                  IsThreeState="False">
                    <DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"  Value="Bold" />
                            <Setter Property="Foreground"  Value="Yellow" />
                            <Setter Property="Background"  Value="Black" />
                        </Style>
                    </DataGridCheckBoxColumn.HeaderStyle>
                </DataGridCheckBoxColumn>

                <DataGridTemplateColumn Header="{lex:LocTextExtension Key=Time, Dict=Resources, Assembly=PreShow.Player}" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Downloaded}" Margin="4"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"  Value="Bold" />
                            <Setter Property="Foreground"  Value="Yellow" />
                            <Setter Property="Background"  Value="Black" />
                        </Style>
                    </DataGridTemplateColumn.HeaderStyle>
                </DataGridTemplateColumn>

                <DataGridCheckBoxColumn 
                  IsReadOnly="True"
                  Header="{lex:LocTextExtension Key=IsDownloading, Dict=Resources, Assembly=PreShow.Player}" 
                  Width="60"
                  Binding="{Binding IsDownloading}"
                  IsThreeState="False">
                    <DataGridCheckBoxColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"  Value="Bold" />
                            <Setter Property="Foreground"  Value="Yellow" />
                            <Setter Property="Background"  Value="Black" />
                        </Style>
                    </DataGridCheckBoxColumn.HeaderStyle>
                </DataGridCheckBoxColumn>

                <DataGridHyperlinkColumn 
                    Header="URL" 
                    Width="Auto"
                    IsReadOnly="True"
                    Binding="{Binding Path=URL}"
                    TargetName="{Binding Path=URL}">
                    <DataGridHyperlinkColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <EventSetter Event="Hyperlink.Click" Handler="OnHyperlinkClick" />
                        </Style>
                    </DataGridHyperlinkColumn.ElementStyle>
                    <DataGridHyperlinkColumn.HeaderStyle>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Setter Property="FontWeight"  Value="Bold" />
                            <Setter Property="Foreground"  Value="Yellow" />
                            <Setter Property="Background"  Value="Black" />
                        </Style>
                    </DataGridHyperlinkColumn.HeaderStyle>
                </DataGridHyperlinkColumn>
            </DataGrid.Columns>
        </DataGrid>