Comment changer la couleur de fond de l'en-tête de colonne lors de l'utilisation de datagrid WPF

comment changer la couleur d'arrière-plan de l'en-tête de colonne en utilisant le datagrid WPF? Besoin de modifier xaml directement?

24
demandé sur iliketocode 2010-12-20 07:54:23

4 réponses

Utilisez un style avec un setter ciblé sur DataGridColumnHeader:

<DataGrid>
    <DataGrid.Resources>
        <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="Background" Value="Blue" />
        </Style>
    </DataGrid.Resources>
</DataGrid>
42
répondu Greg Sansom 2017-10-29 21:39:07

Utilisation:

  <windows.Resources>
      <LinearGradientBrush x:Key="HeaderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#FF6B8E95" Offset="0"/>
                <GradientStop Color="#FF14A7C1" Offset="1"/>
                <GradientStop Color="#FF1E424E" Offset="0.509"/>
                <GradientStop Color="#FF1D4855" Offset="0.542"/>
                <GradientStop Color="#FF1D4855" Offset="0.542"/>
                <GradientStop Color="#FF193A44" Offset="0.526"/>
            </LinearGradientBrush>

            <LinearGradientBrush x:Key="HeaderBorderBrush" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Color="#FF1D1D1D" Offset="0.614"/>
                <GradientStop Color="#FF007F96" Offset="0.853"/>
                <GradientStop Color="#FF0AEAFA" Offset="1"/>
            </LinearGradientBrush>

            <Style x:Key="HeaderStyle" TargetType="DataGridColumnHeader">
                <Setter Property="Background" Value="{StaticResource HeaderBrush}" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="BorderBrush" Value="{StaticResource HeaderBorderBrush}" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="HorizontalContentAlignment" Value="Left" />
                <Setter Property="MinWidth" Value="0" />
                <Setter Property="MinHeight" Value="30" />
                <Setter Property="Cursor" Value="Hand" />
            </Style>
    </Windows.Resources>

<Grid>
<DataGrid Name="dataGrid1" ColumnHeaderStyle="{StaticResource HeaderStyle}"/>
</Grid>

voici le résultat:

result

4
répondu Jeetendra negi 2016-04-05 17:01:25
var style = new Style(typeof(System.Windows.Controls.Primitives
    .DataGridColumnHeader));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty
    ,"Your tool tip here"));
style.Setters.Add(new Setter { Property = BackgroundProperty, Value 
    = Brushes.Yellow });


dgExcelSheet.Columns[1].HeaderStyle = style;
3
répondu Olof 2014-01-09 10:50:06
<DataGridTextColumn Header="ID">
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
1
répondu iliketocode 2017-05-24 19:50:34