简体   繁体   English

如何格式化WPF Datagrid上的标头?

[英]How do I format headers on a WPF Datagrid?

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 eg 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 eg change the Datagrid Header background to orange on this DataGrid? 我怎样才能在此DataGrid上将Datagrid标题背景更改为橙色?

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

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>

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 基本上它在模板中的另一个地方定义:这将解释TemlateBinding给你的MSDN TemplateBinding

HTH, Eric HTH,Eric

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM