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

31882d7e2ed2301148b26201e9d689bd.png

扫一扫关注我们,

25caaac2d407d66f9b91817e3c9802e6.gif

更多知识早知道!

1c2aa29f4c5c239908b3a3063afaddfb.gif

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF自定义DataGrid控件是通过继承现有的DataGrid类,并在其基础上进行修改和扩展来实现的。 自定义DataGrid控件的步骤如下: 1. 创建一个自定义的类,继承自DataGrid。例如,可以命名为CustomDataGrid。 2. 在CustomDataGrid类中,可以添加额外的属性、依赖属性或附加属性,用于自定义DataGrid控件的特定行为或外观。 3. 重写或扩展DataGrid的现有方法、事件和样式,以满足自定义需求。例如,可以重写OnApplyTemplate()方法以应用自定义样式。 4. 根据需要,可以添加新的功能或控件,例如自定义列、单元格、行、排序、筛选、分页等等。 5. 在CustomDataGrid类中,可以通过编写自定义的模板(Template)来修改DataGrid的外观。例如,可以通过修改DataGrid的ControlTemplate来改变整个DataGrid的显示风格。 6. 编写完自定义类后,可以在XAML中使用自定义DataGrid控件,通过添加命名空间引用并将CustomDataGrid作为一个控件使用。可以设置自定义属性、事件和样式,达到期望的效果。 通过自定义DataGrid控件,可以根据实际需求对其进行扩展和修改,以满足特定的业务需求。由于WPF提供了强大的样式、模板和继承机制,因此可以轻松地自定义DataGrid控件,并且可以实现高度的灵活性和可扩展性。这样,可以根据项目的需求和用户的喜好来创建独特的、具有个性化的DataGrid控件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值