WPF 基础控件之 DataGrid 样式

Posted dotNET跨平台

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 基础控件之 DataGrid 样式相关的知识,希望对你有一定的参考价值。

此群已满340500857 ,请加新群458041663

       由于微信群人数太多入群请添加小编微信号

 yanjinhuawechatW_Feng_aiQ 邀请入群

 需备注WPF开发者 

 PS:有更好的方式欢迎推荐。

支持Nuget

Install-Package WPFDevelopers.Minimal -Version 3.2.0

01

代码如下

一、创建 Styles.DataGrid.xaml 代码如下。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Themes/Basic/ControlBasic.xaml"/>
        <ResourceDictionary Source="../Themes/Basic/Animations.xaml"/>
        <ResourceDictionary Source="Styles.ScrollBar.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!--Style and template for the button in the upper left corner of the DataGrid.-->
    <Style TargetType="x:Type Button"
       x:Key="ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, 
  TypeInTargetAssembly=x:Type DataGrid" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type Button">
                    <Border Background="DynamicResource WhiteSolidColorBrush"
                            BorderThickness="0,0,0,1"
                            BorderBrush="DynamicResource BaseSolidColorBrush">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <!--<Polygon x:Name="Arrow"
                   HorizontalAlignment="Right"
                   Margin="8,8,3,3"
                   Opacity="0.15"
                   Points="0,10 10,10 10,0"
                   Stretch="Uniform"
                   VerticalAlignment="Bottom">
                            <Polygon.Fill>
                                <SolidColorBrush Color="DynamicResource PrimaryTextColor" />
                            </Polygon.Fill>
                        </Polygon>-->
                        <Polygon x:Name="Arrow" 
                                 Opacity="1"
                                 Margin="0,2,0,0"
                                 Points="0,10 10,10 10,0"
                                 Stretch="Uniform"
                                 VerticalAlignment="Center">
                            <Polygon.Fill>
                                <SolidColorBrush Color="DynamicResource PrimaryTextColor" />
                            </Polygon.Fill>
                        </Polygon>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Fill" Value="DynamicResource PrimaryMouseOverSolidColorBrush" TargetName="Arrow"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGrid.-->
    <Style TargetType="x:Type DataGrid" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Foreground" Value="DynamicResource PrimaryTextSolidColorBrush" />
        <Setter Property="BorderThickness" Value="1,1,1,0"/>
        <Setter Property="BorderBrush" Value="DynamicResource BaseSolidColorBrush"/>
        <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
        <Setter Property="ScrollViewer.PanningMode" Value="Both" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="HorizontalGridLinesBrush" Value="x:Null"/>
        <Setter Property="VerticalGridLinesBrush" Value="x:Null"/>
        <Setter Property="MinRowHeight" Value="DynamicResource MinDataGridRowHeight"/>
        <Setter Property="CanUserAddRows" Value="False"/>
        <Setter Property="AutoGenerateColumns" Value="False"/>
        <Setter Property="RowHeaderWidth" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGrid">
                    <Border x:Name="border"
                SnapsToDevicePixels="True"
                BorderBrush="TemplateBinding BorderBrush"
                BorderThickness="TemplateBinding BorderThickness"
                Padding="TemplateBinding Padding">
                        <Border.Background>
                            <SolidColorBrush Color="DynamicResource WhiteColor" />
                        </Border.Background>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="Normal" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
                            <ScrollViewer.Template>
                                <ControlTemplate TargetType="x:Type ScrollViewer">
                                    <Grid>
                                        <!--<Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>-->
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="*" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>

                                        <Button Focusable="False"
                          Command="x:Static DataGrid.SelectAllCommand"
                          Style="DynamicResource ComponentResourceKey 
                      ResourceId=DataGridSelectAllButtonStyle, 
                      TypeInTargetAssembly=x:Type DataGrid"
                          Visibility="Binding HeadersVisibility, 
                      ConverterParameter=x:Static DataGridHeadersVisibility.All, 
                      Converter=x:Static DataGrid.HeadersVisibilityConverter, 
                      RelativeSource=RelativeSource AncestorType=x:Type DataGrid"
                          Width="Binding CellsPanelHorizontalOffset, 
                      RelativeSource=RelativeSource AncestorType=x:Type DataGrid" />

                                        <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
                                                  Grid.Column="1" 
                                                  Visibility="Binding HeadersVisibility, 
                      ConverterParameter=x:Static DataGridHeadersVisibility.Column, 
                      Converter=x:Static DataGrid.HeadersVisibilityConverter, 
                      RelativeSource=RelativeSource AncestorType=x:Type DataGrid" />

                                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                          Grid.ColumnSpan="2"
                                          Grid.Row="1"
                                          CanContentScroll="TemplateBinding CanContentScroll" />

                                        <ScrollBar x:Name="PART_VerticalScrollBar"
                             Grid.Column="1" HorizontalAlignment="Right"
                             Grid.Row="1"
                             Orientation="Vertical"
                             ViewportSize="TemplateBinding ViewportHeight"
                             Maximum="TemplateBinding ScrollableHeight"
                             Visibility="TemplateBinding ComputedVerticalScrollBarVisibility"
                             Value="Binding VerticalOffset, Mode=OneWay, 
                      RelativeSource=RelativeSource TemplatedParent"/>

                                        <Grid Grid.Column="1"
                        Grid.Row="2">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="Binding NonFrozenColumnsViewportHorizontalOffset, 
                          RelativeSource=RelativeSource AncestorType=x:Type DataGrid" />
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>

                                            <ScrollBar x:Name="PART_HorizontalScrollBar"
                               Grid.Column="1"
                               Orientation="Horizontal"
                               ViewportSize="TemplateBinding ViewportWidth"
                               Maximum="TemplateBinding ScrollableWidth"
                               Visibility="TemplateBinding ComputedHorizontalScrollBarVisibility"
                               Value="Binding HorizontalOffset, Mode=OneWay, 
                        RelativeSource=RelativeSource TemplatedParent"/>
                                        </Grid>
                                    </Grid>
                                </ControlTemplate>
                            </ScrollViewer.Template>
                            <ItemsPresenter SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" />
                        </ScrollViewer>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsGrouping" Value="True">
                <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <!--Style and template for the DataGridCell.-->
    <Style TargetType="x:Type DataGridCell" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGridCell">
                    <Border x:Name="border"
                BorderBrush="Transparent"
                BorderThickness="1" 
                Background="Transparent"
                SnapsToDevicePixels="True">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Unfocused" />
                                <VisualState x:Name="Focused" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CurrentStates">
                                <VisualState x:Name="Regular" />
                                <VisualState x:Name="Current"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" 
                                              VerticalAlignment="Center" 
                                              HorizontalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsSelected" Value="True"/>
                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="Foreground" Value="DynamicResource PrimaryTextSolidColorBrush"/>
                <Setter Property="Background" Value="DynamicResource BaseSolidColorBrush" />
            </MultiTrigger>

        </Style.Triggers>
    </Style>

    <!--Style and template for the DataGridRow.-->
    <Style TargetType="x:Type DataGridRow" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Validation.ErrorTemplate" Value="x:Null" />
        <Setter Property="BorderThickness" Value="0,0,0,1"/>
        <Setter Property="BorderBrush" Value="DynamicResource BaseSolidColorBrush"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGridRow">
                    <Border x:Name="DGR_Border"
                                BorderBrush="TemplateBinding BorderBrush"
                                BorderThickness="TemplateBinding BorderThickness"
                                SnapsToDevicePixels="True" >
                        <Border.Background>
                            <SolidColorBrush Color="DynamicResource WhiteColor"/>
                        </Border.Background>

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Normal_AlternatingRow"/>
                                <VisualState x:Name="Normal_Selected"/>
                                <VisualState x:Name="Unfocused_Selected"/>
                                <VisualState x:Name="Normal_Editing"/>
                                <VisualState x:Name="MouseOver_Editing">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="DGR_Border"
                                                Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" To="DynamicResource DefaultBackgroundColor">
                                        </ColorAnimation>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver_Unfocused_Editing"/>
                                <VisualState x:Name="Unfocused_Editing"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="DGR_Border" Duration="0:0:0.2"
                                                Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" To="DynamicResource BaseMoveColor">
                                        </ColorAnimation>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver_Selected">
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="DGR_Border" Duration="0:0:0.2"
                                                Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" To="DynamicResource BaseMoveColor">
                                        </ColorAnimation>
                                        <!--<DoubleAnimation Storyboard.TargetName="PART_MouseOver" Duration="0:0:0.3"
                                                             Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleX)"
                                                             To="1"/>
                                            <DoubleAnimation Storyboard.TargetName="PART_MouseOver" Duration="0:0:0.01" BeginTime="0:0:0.3"
                                                             Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleX)"
                                                             To="0"/>-->

                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="MouseOver_Unfocused_Selected"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <!--<Grid>
                                -->
                        <!--<Rectangle x:Name="PART_MouseOver"
                                           Fill="DynamicResource PrimaryMouseOverSolidColorBrush"
                                           RenderTransformOrigin="0,1">
                                    <Rectangle.RenderTransform>
                                        <ScaleTransform ScaleY="1" ScaleX="0"/>
                                    </Rectangle.RenderTransform>
                                </Rectangle>-->
                        <!--
                               
                            </Grid>-->
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1"
                                    ItemsPanel="TemplateBinding ItemsPanel"
                                    SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" />
                            <DataGridDetailsPresenter Grid.Column="1"
                                      Grid.Row="1"
                                      Visibility="TemplateBinding DetailsVisibility"
                                      SelectiveScrollingGrid.SelectiveScrollingOrientation=
                "Binding AreRowDetailsFrozen, 
                ConverterParameter=x:Static SelectiveScrollingOrientation.Vertical,
                Converter=x:Static DataGrid.RowDetailsScrollingConverter, 
                RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
                            <DataGridRowHeader Grid.RowSpan="2"
                               SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                               Visibility="Binding HeadersVisibility, 
                ConverterParameter=x:Static DataGridHeadersVisibility.Row, 
                Converter=x:Static DataGrid.HeadersVisibilityConverter, 
                RelativeSource=RelativeSource AncestorType=x:Type DataGrid" />
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!--<Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="DynamicResource BaseSolidColorBrush" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DynamicResource BaseSolidColorBrush" />
                </Trigger>
             
            </Style.Triggers>-->
    </Style>

    <!--Style and template for the resize control on the DataGridRowHeader.-->
    <Style x:Key="RowHeaderGripperStyle"
       TargetType="x:Type Thumb" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Height"
          Value="8" />
        <Setter Property="Background"
          Value="Transparent" />
        <Setter Property="Cursor"
          Value="SizeNS" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type Thumb">
                    <Border Background="TemplateBinding Background"
                Padding="TemplateBinding Padding" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGridRowHeader.-->
    <Style TargetType="x:Type DataGridRowHeader">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGridRowHeader">
                    <Grid HorizontalAlignment="Center">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Normal_CurrentRow" />
                                <VisualState x:Name="Unfocused_EditingRow" />
                                <VisualState x:Name="Normal_EditingRow" />
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="MouseOver_CurrentRow" />
                                <VisualState x:Name="MouseOver_Unfocused_EditingRow" />
                                <VisualState x:Name="MouseOver_EditingRow" />
                                <VisualState x:Name="MouseOver_Unfocused_Selected" />
                                <VisualState x:Name="MouseOver_Selected" />
                                <VisualState x:Name="MouseOver_Unfocused_CurrentRow_Selected" />
                                <VisualState x:Name="MouseOver_CurrentRow_Selected" />
                                <VisualState x:Name="Unfocused_Selected" />
                                <VisualState x:Name="Unfocused_CurrentRow_Selected" />
                                <VisualState x:Name="Normal_CurrentRow_Selected" />
                                <VisualState x:Name="Normal_Selected" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="rowHeaderBorder" 
                                    Background="Transparent">

                            <StackPanel Orientation="Horizontal">
                                <ContentPresenter VerticalAlignment="Center"
                                SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" />
                                <Control SnapsToDevicePixels="false"
                       Template="Binding ValidationErrorTemplate, 
                  RelativeSource=RelativeSource AncestorType=x:Type DataGridRow"
                       Visibility="Binding (Validation.HasError), 
                  Converter=StaticResource bool2VisibilityConverter, 
                  RelativeSource=RelativeSource AncestorType=x:Type DataGridRow" />
                            </StackPanel>
                        </Border>

                        <Thumb x:Name="PART_TopHeaderGripper"
                 Style="StaticResource RowHeaderGripperStyle"
                 VerticalAlignment="Top" />
                        <Thumb x:Name="PART_BottomHeaderGripper"
                 Style="StaticResource RowHeaderGripperStyle"
                 VerticalAlignment="Bottom" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the resize control on the DataGridColumnHeader.-->
    <Style x:Key="ColumnHeaderGripperStyle"
       TargetType="x:Type Thumb" BasedOn="StaticResource ControlBasicStyle">
        <!--<Setter Property="Width"
          Value="8" />
        <Setter Property="Background"
          Value="Transparent" />
        <Setter Property="Cursor"
          Value="SizeWE" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type Thumb">
                    <Border Background="TemplateBinding Background"
                Padding="TemplateBinding Padding" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>-->
        <Setter Property="Width" Value="8" />
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Cursor" Value="SizeWE" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type Thumb">
                    <Border Padding="TemplateBinding Padding" Background="Transparent">
                        <Rectangle HorizontalAlignment="Center" Width="1"  Fill="TemplateBinding Background" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="DynamicResource PrimaryMouseOverSolidColorBrush"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGridColumnHeader.-->
    <Style TargetType="x:Type DataGridColumnHeader" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGridColumnHeader">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="StoryboardAscending">
                            <DoubleAnimation Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)"
                                                 Storyboard.TargetName="SortArrow"
                                                 Duration="00:00:.2" To="180"/>
                        </Storyboard>
                        <Storyboard x:Key="StoryboardDescending">
                            <DoubleAnimation Storyboard.TargetProperty="(Path.RenderTransform).(RotateTransform.Angle)"
                                                 Storyboard.TargetName="SortArrow"
                                                 Duration="00:00:.2" To="0"/>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>
                        <Border x:Name="columnHeaderBorder" 
                                    Background="DynamicResource WhiteSolidColorBrush"
                                    Padding="10"
                                    BorderThickness="0,0,0,1" BorderBrush="DynamicResource BaseSolidColorBrush">
                            <Grid Name="HedearGrid">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <ContentPresenter HorizontalAlignment="TemplateBinding HorizontalContentAlignment"
                                                  SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" 
                                                  VerticalAlignment="TemplateBinding VerticalContentAlignment" 
                                                  TextElement.FontWeight="ExtraBlack"/>
                                <Path x:Name="SortArrow" Visibility="Collapsed"  Data="StaticResource PathSortArrow" 
                                          Stretch="Fill" 
                                          Grid.Column="1" Width="10" Height="10" Fill="DynamicResource PrimaryPressedSolidColorBrush"
                                          Margin="4,0,0,0"
                                          VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" >
                                    <Path.RenderTransform>
                                        <RotateTransform/>
                                    </Path.RenderTransform>
                                </Path>
                            </Grid>
                        </Border>

                        <Thumb x:Name="PART_LeftHeaderGripper"
                 HorizontalAlignment="Left"
                 Style="StaticResource ColumnHeaderGripperStyle" />
                        <Thumb x:Name="PART_RightHeaderGripper"
                 HorizontalAlignment="Right"
                 Style="StaticResource ColumnHeaderGripperStyle" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="SortDirection" Value="Ascending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="StoryboardAscendingBeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="StoryboardAscendingBeginStoryboard" Storyboard="StaticResource StoryboardAscending" />
                            </Trigger.EnterActions>
                            <!--<Setter TargetName="SortArrow" Property="RenderTransform">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" />
                                    </Setter.Value>
                                </Setter>-->
                            <!--<Setter TargetName="SortArrow" Property="Margin" Value="4,4,0,0"/>-->
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            <Trigger.ExitActions>
                                <StopStoryboard BeginStoryboardName="StoryboardDescendingBeginStoryboard" />
                            </Trigger.ExitActions>
                            <Trigger.EnterActions>
                                <BeginStoryboard x:Name="StoryboardDescendingBeginStoryboard" Storyboard="StaticResource StoryboardDescending" />
                            </Trigger.EnterActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--Style and template for the DataGridColumnHeadersPresenter.-->
    <Style TargetType="x:Type DataGridColumnHeadersPresenter" BasedOn="StaticResource ControlBasicStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type DataGridColumnHeadersPresenter">
                    <Grid>
                        <DataGridColumnHeader x:Name="PART_FillerColumnHeader"
                                IsHitTestVisible="False" />
                        <ItemsPresenter />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

二、使用 Styles.DataGrid.xaml  代码如下。

<WrapPanel Margin="0,10">
          <DataGrid AutoGenerateColumns="False" HeadersVisibility="All" RowHeaderWidth="40"
                                  ItemsSource="Binding UserCollection,RelativeSource=RelativeSource AncestorType=local:MainView"
                                  Margin="0,10">
                            <DataGrid.RowHeaderTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="Binding IsSelected,RelativeSource=RelativeSource AncestorType=DataGridRow"/>
                                </DataTemplate>
                            </DataGrid.RowHeaderTemplate>
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Date" Binding="Binding Date" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Name" Binding="Binding Name" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Address" Binding="Binding Address" IsReadOnly="True"/>
                            </DataGrid.Columns>
                        </DataGrid>
 </WrapPanel>

 <WrapPanel Margin="0,10">
        <DataGrid AutoGenerateColumns="False" 
                                  ItemsSource="Binding UserCollection,RelativeSource=RelativeSource AncestorType=local:MainView"
                                  Margin="0,10">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn CanUserResize="False">
                                <DataGridTemplateColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="Binding RelativeSource=RelativeSource Mode=FindAncestor, AncestorType=local:MainView, Path=AllSelected" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox IsChecked="Binding IsChecked"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                                <DataGridTextColumn Header="Date" Binding="Binding Date" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Name" Binding="Binding Name" IsReadOnly="True"/>
                                <DataGridTextColumn Header="Address" Binding="Binding Address" IsReadOnly="True"/>
                            </DataGrid.Columns>
                        </DataGrid>
 </WrapPanel>

02


效果预览

鸣谢素材提供者element

源码地址如下

Github:https://github.com/WPFDevelopersOrg

Gitee:https://gitee.com/WPFDevelopersOrg

https://gitee.com/WPFDevelopersOrg/WPFDevelopers.Minimal

https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal

WPF开发者QQ群: 340500857 

Github:https://github.com/WPFDevelopersOrg

出处:https://www.cnblogs.com/yanjinhua

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

转载请著名作者 出处 https://github.com/WPFDevelopersOrg

扫一扫关注我们,

更多知识早知道!

点击阅读原文可跳转至源代码

以上是关于WPF 基础控件之 DataGrid 样式的主要内容,如果未能解决你的问题,请参考以下文章

WPF 基础控件之 ToggleButton 样式

WPF 基础控件之 Slider 样式

WPF 基础控件之 GroupBox样式

WPF 基础控件之 RadioButton 样式

WPF日积月累之DataGrid样式以及操作数据模板中的控件

WPF自定义控件与样式-列表控件DataGrid与ListView自定义样式