如何更改datagrid中列标题行的颜色和格式?

时间:2019-04-30 15:54:42

标签: wpf xaml datagridview wpfdatagrid

我尝试寻找解决方案,但大多数已过时且当前不可用。例如

{% if language_code == 'en' %}
    {% set urlBase = '' %}
{% else %}
    {% set urlBase = '/' ~ language_code %}
{% endif %}
<!DOCTYPE html>
<html lang="{{ language_code }}"
    {% if language_code == 'ar' or language_code =='ur' %}dir="rtl"{% endif %}
    {% if is_print %}class="print-view"{% endif %}>
  <head>
    {% block html_head_container %}
      {{ function('enqueue_language_style', 'style') }}
      {% include 'partials/html-header.twig' %}
      {% block head %}
      <meta name="theme-color" content="#FFFFFF">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="manifest" href="{{theme.path}}/manifest.960190a2.json">
      {% endblock %}
      {% block scripts_head %}
      {% include 'partials/scripts-field-segment.twig' %}
      {% include 'partials/scripts-google-analytics.twig' %}
      {% endblock %}
    {% endblock %}
  </head>

不再有columnheadesdefaultcellstyle。

2 个答案:

答案 0 :(得分:0)

对于WPF数据网格,可以设置ColumnHeaderStyle,您可以在其中更改标题行的外观。

   <Style x:Key="FADataGridStyle" TargetType="{x:Type DataGrid}">
      <Setter Property="RowHeaderWidth" Value="0"/>
      <Setter Property="BorderBrush" Value="Transparent"/>
      <Setter Property="BorderThickness" Value="0"/>
      <Setter Property="GridLinesVisibility" Value="Horizontal"/>
      <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource BackgroundColorBrush}"/>
      <Setter Property="ColumnHeaderStyle" Value="{DynamicResource FADataGridColumnHeaderStyle}"/>
      <Setter Property="SelectionUnit" Value="FullRow"/>
   </Style>

   <Style x:Key="FADataGridColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="Height" Value="50"/>
      <Setter Property="FontSize" Value="14"/>
      <Setter Property="HorizontalAlignment" Value="Stretch"/>
      <Setter Property="HorizontalContentAlignment" Value="Center"/>
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="FontFamily" Value="{StaticResource ApplicationLightFont}"/>
      <Setter Property="BorderThickness" Value="0,0,0,1"/>
   </Style>

现在,在XAML数据网格定义上,您可以设置样式。 (请注意FADataGridStyle)

  <DataGrid AutoGenerateColumns="False"
               CanUserAddRows="False"
               CanUserSortColumns="True"
               CanUserReorderColumns="True"
               ItemsSource="{Binding Recordings}"
               RowHeaderWidth="0"
               SelectionMode="Single"
               ScrollViewer.IsDeferredScrollingEnabled="True"
               Background="{StaticResource SurfaceColorBrush}"
               Style="{StaticResource FADataGridStyle}">

我相信这取决于您的要求。

enter image description here

答案 1 :(得分:0)

<DataGrid Grid.Column="1" Grid.Row="1" ItemsSource="{Binding ColTips.FilteredPayments}" AutoGenerateColumns="False" IsReadOnly="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Date" Binding="{Binding Path=PaymentDate}">
                        <DataGridTextColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="Background" Value="Red"></Setter>
                            </Style>
                        </DataGridTextColumn.HeaderStyle>
                    </DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>

在这里,您只需在所需列上设置样式即可内联。