WPF下DataGrid样式

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

    <sys:Double x:Key="DataGridRow.Height">33sys:Double>

    
    <Thickness x:Key="DataGrid.BorderThickness" Bottom="1" Left="1" Right="1" Top="1" />
    
    <Thickness x:Key="ColumnHeader.BorderThickness" Bottom="0" Left="0" Right="1" Top="0" />
    
    <Thickness x:Key="DataGridRow.BorderThickness" Bottom="0" Left="0" Right="0" Top="1" />

    
    <SolidColorBrush x:Key="DataGrid.BorderBrush" Color="#E9ECF1" />
    
    <SolidColorBrush x:Key="ColumnHeader.Background" Color="#F6F7FB" />
    
    <SolidColorBrush x:Key="ColumnHeader.BorderBrush" Color="#E9ECF1" />
    
    <SolidColorBrush x:Key="DataGridRow.BorderBrush" Color="#E9ECF1" />
    
    <SolidColorBrush x:Key="DataGridRow.Normal.Background" Color="#FFFFFF" />
    
    <SolidColorBrush x:Key="DataGridRow.Normal.Foreground" Color="#000000" />
    
    <SolidColorBrush x:Key="DataGridRow.MouseOver.Background" Color="#F6F7FB" />
    
    <SolidColorBrush x:Key="DataGridRow.MouseOver.Foreground" Color="#000000" />
    
    <SolidColorBrush x:Key="DataGridRow.Selected.Background" Color="#F6F7FB" />
    
    <SolidColorBrush x:Key="DataGridRow.Selected.Foreground" Color="#000000" />

    <Style TargetType="DataGrid">
        --  网格线颜色  -->
        <Setter Property="RowHeaderWidth" Value="0" />
        <Setter Property="BorderThickness" Value="{StaticResource DataGrid.BorderThickness}" />
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    Style>
    
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Foreground" Value="#000000" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Height" Value="28" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridColumnHeader">
                    <Border x:Name="BackgroundBorder" Width="Auto" Margin="-1,0"
                            BorderBrush="{StaticResource ColumnHeader.BorderBrush}"
                            BorderThickness="{StaticResource ColumnHeader.BorderThickness}"
                            SnapsToDevicePixels="True" UseLayoutRounding="True">
                        
                    
                
            
        
    Style>
    
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{StaticResource DataGridRow.Normal.Background}" />
        
        
        
        Null}" />
        <Setter Property="BorderThickness" Value="{StaticResource DataGridRow.BorderThickness}" />
        
        
            
                
                    
                
            
        
        
            
                Type DataGridRow}">
                    <Border x:Name="DGR_Border" Margin="0,0,0,-1"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            SnapsToDevicePixels="True" UseLayoutRounding="True">
                        
                            
                                
                                
                            
                            
                                
                                
                            
                            
                            Static SelectiveScrollingOrientation.Vertical},
                                                                                                                     Converter={x:Static DataGrid.RowDetailsScrollingConverter},
                                                                                                                     RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                      Visibility="{TemplateBinding DetailsVisibility}" />
                            Static DataGridHeadersVisibility.Row},
                                                                    Converter={x:Static DataGrid.HeadersVisibilityConverter},
                                                                    RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                        SelectiveScrollingGrid>
                    Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource DataGridRow.MouseOver.Foreground}" />
                            
                        
                        
                            
                            
                        
                    
                
            
        
    Style>
    
    <Style TargetType="DataGridCell">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Border x:Name="Bg" Background="Transparent" UseLayoutRounding="True">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    Border>
                ControlTemplate>
            Setter.Value>
        Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="#000000" />
            Trigger>
        Style.Triggers>
    Style>
ResourceDictionary>

效果:
WPF下DataGrid样式_第1张图片

单元格增加气泡提示方法:

<DataGridTemplateColumn Width="5*" Header="机构名称">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding OrgName}" ToolTip="{Binding OrgName}" />
        DataTemplate>
    DataGridTemplateColumn.CellTemplate>
DataGridTemplateColumn>

你可能感兴趣的:(桌面开发(C/S))