WPF #DataGrid样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:controls="clr-namespace:ITL.Presentation.Controls">
  
    <!--单元格默认样式-->
    <Style x:Key="DataGridCellDefaultStyle" TargetType="DataGridCell" >
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="Foreground" Value="{DynamicResource DataGridCellForeground}"/>
        <Setter Property="Focusable" Value="True" />
        <Setter Property="FontWeight" Value="Normal"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Border Background="Transparent" BorderBrush="Transparent" BorderThickness="1"
                            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"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!--行默认样式-->
    <Style x:Key="DataGridRowDefaultStyle" TargetType="DataGridRow">
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="Background" Value="Transparent"/>
        <!--<Setter Property="Background" Value="{StaticResource RowBackground}" />-->
        <Setter Property="BorderBrush" Value="{DynamicResource RowBorderBrush}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>

        <!--<Setter Property="Margin" Value="0,0"/>-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <Border  BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
                        <SelectiveScrollingGrid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="0"/>
                                <ColumnDefinition Width="*"/>
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="auto"/>
                                <RowDefinition Height="auto"/>
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridRowHeader Grid.Row="0" Grid.Column="0" SnapsToDevicePixels="True"/>
                            <DataGridCellsPresenter Grid.Row="0" Height="{TemplateBinding Height}" Grid.Column="1" SnapsToDevicePixels="True" VerticalAlignment="{TemplateBinding VerticalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" SnapsToDevicePixels="True" Visibility="{TemplateBinding DetailsVisibility}"/>
                        </SelectiveScrollingGrid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="AlternationIndex" Value="0">
                <Setter Property="Background"  Value="#66196D91"/>
            </Trigger>
            <Trigger Property="AlternationIndex" Value="1">
                <Setter Property="Background" Value="#00196D91"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{DynamicResource RowMouseOverBackground}"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource RowSelectedBackground}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <SolidColorBrush x:Key="DataGridHeaderColor" Color="{DynamicResource AccentColor}" Opacity=".05"></SolidColorBrush>

    <SolidColorBrush x:Key="DataGridBackground" Color="Transparent" />
    <SolidColorBrush x:Key="DataGridForeground" Color="#d1d1d1" />
    <SolidColorBrush x:Key="DataGridCellBackground" Color="Transparent" />
    <SolidColorBrush x:Key="DataGridCellBackgroundSelected" Color="{DynamicResource AccentColor}" />
    
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Background" Value="{StaticResource DataGridHeaderColor}" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Height" Value="55" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontFamily" Value="微软雅黑" />
        <Setter Property="FontSize" Value="18" />
        <Setter Property="Padding" Value="10,3,10,3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Padding ="{TemplateBinding Padding}"
>
                            <Grid x:Name="containerGrid" Margin="5" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Grid.Column="0"
                                                  HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <Path x:Name="SortArrow"
                                      Grid.Column="1"
                                      HorizontalAlignment="Center" VerticalAlignment="Center"
                                      Width="8" Height="6"  Margin="5,0,5,0"
                                      Stretch="Fill" Opacity="0.5" Fill="{TemplateBinding Foreground}"
                                      RenderTransformOrigin="0.5,0.4"
                                      Visibility="Hidden"
                                      Data="M0,0 L1,0 0.5,1 z" />
                            </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" />
                            <Setter TargetName="SortArrow" Property="RenderTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="180" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="SortDirection" Value="Descending">
                            <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundHover}" />
                <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForegroundHover}" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundPressed}" />
                <Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForegroundPressed}" />
            </Trigger>
        </Style.Triggers>

    </Style>

    <Style TargetType="{x:Type controls:DataGrid}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="BorderBrush" Value="#0F6DD8" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="AlternationCount" Value="2" />
        <Setter Property="AutoGenerateColumns" Value="False" />
        <Setter Property="CanUserReorderColumns" Value="true" />
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="CanUserResizeRows" Value="False" />
        <Setter Property="GridLinesVisibility" Value="None" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="Height" Value="auto"/>
        <Setter Property="CanUserAddRows" Value="False"></Setter>
        <Setter Property="RowHeaderWidth" Value="0" />
        <Setter Property="SelectionMode" Value="Single"></Setter>
        <Setter Property="FontFamily" Value="微软雅黑"/>
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="CellStyle" Value="{StaticResource DataGridCellDefaultStyle}" />
        <Setter Property="RowStyle" Value="{StaticResource DataGridRowDefaultStyle}" />
        <Setter Property="RowHeight" Value="40"/>
    </Style>

    <Style x:Key="DataGridEditingComboBoxStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}">
        <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Padding" Value="1,1" />
    </Style>

    <Style x:Key="DataGridCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
        <Setter Property="IsHitTestVisible" Value="False" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

    <Style x:Key="DataGridEditingCheckBoxStyle" TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

    <Style x:Key="DataGridTextStyle" TargetType="TextBlock" >
        <Setter Property="Margin" Value="5,0,5,0" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="ToolTip" Value="{Binding Path=Text,RelativeSource={RelativeSource Mode=Self}}"/>
    </Style>

    <Style x:Key="DataGridOrderTextStyle" TargetType="TextBlock" >
        <Setter Property="Margin" Value="10,0,0,0" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="FontStyle" Value="Italic"/>
        <Setter Property="ToolTip" Value="{Binding Path=Text,RelativeSource={RelativeSource Mode=Self}}"/>
        <Setter Property="Effect">
            <Setter.Value>
                <DropShadowEffect Color="Black" BlurRadius="5" ShadowDepth="2" Opacity=".6"/>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="DataGridEditingTextStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Margin" Value="5,0,5,0" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0" />
        <Setter Property="MinWidth" Value="45" />
        <Setter Property="ToolTip" Value="{Binding Path=Text, RelativeSource={RelativeSource Mode=Self}}"/>
    </Style>

</ResourceDictionary>

  

posted on 2021-07-30 14:39  小熊博客园  阅读(276)  评论(0)    收藏  举报

导航