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">33</sys: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}" />
        <Setter Property="HeadersVisibility" Value="Column" />
        <Setter Property="Background" Value="{StaticResource ColumnHeader.Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource DataGrid.BorderBrush}" />
        <Setter Property="HorizontalGridLinesBrush" Value="#00E9ECF1" />
        <Setter Property="VerticalGridLinesBrush" Value="#00E9ECF1" />
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="AutoGenerateColumns" Value="False" />
        <Setter Property="CanUserAddRows" Value="False" />
        <Setter Property="CanUserReorderColumns" Value="False" />
        <Setter Property="CanUserResizeColumns" Value="False" />
        <Setter Property="CanUserResizeRows" Value="False" />
        <Setter Property="CanUserSortColumns" Value="False" />
        <Setter Property="GridLinesVisibility" Value="None" />
        <Setter Property="IsReadOnly" Value="True" />
        <Setter Property="RowHeight" Value="{StaticResource DataGridRow.Height}" />
        <Setter Property="SelectionMode" Value="Single" />
    </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">
                        <ContentPresenter Margin="5,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--  行樣式觸發  背景色改變必須先設置cellStyle 因爲cellStyle會覆蓋rowStyle樣式  -->
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{StaticResource DataGridRow.Normal.Background}" />
        <Setter Property="Foreground" Value="{StaticResource DataGridRow.MouseOver.Foreground}" />
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="UseLayoutRounding" Value="True" />
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
        <Setter Property="BorderThickness" Value="{StaticResource DataGridRow.BorderThickness}" />
        <Setter Property="BorderBrush" Value="{StaticResource DataGridRow.BorderBrush}" />
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Margin="0,0,0,0" VerticalAlignment="Center" Foreground="Red" Text="!" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x: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">
                        <SelectiveScrollingGrid>
                            <SelectiveScrollingGrid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </SelectiveScrollingGrid.ColumnDefinitions>
                            <SelectiveScrollingGrid.RowDefinitions>
                                <RowDefinition Height="*" MinHeight="{StaticResource DataGridRow.Height}" />
                                <RowDefinition Height="Auto" />
                            </SelectiveScrollingGrid.RowDefinitions>
                            <DataGridCellsPresenter Grid.Column="1"
                                                    ItemsPanel="{TemplateBinding ItemsPanel}"
                                                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            <DataGridDetailsPresenter Grid.Row="1" Grid.Column="1"
                                                      SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen,
                                                                                                                     ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical},
                                                                                                                     Converter={x:Static DataGrid.RowDetailsScrollingConverter},
                                                                                                                     RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                      Visibility="{TemplateBinding DetailsVisibility}" />
                            <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.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource DataGridRow.MouseOver.Foreground}" />
                            <Setter Property="Background" Value="{StaticResource DataGridRow.MouseOver.Background}" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="{StaticResource DataGridRow.Selected.Foreground}" />
                            <Setter Property="Background" Value="{StaticResource DataGridRow.Selected.Background}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </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>

效果:
這裏寫圖片描述

單元格增加氣泡提示方法:

<DataGridTemplateColumn Width="5*" Header="機構名稱">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding OrgName}" ToolTip="{Binding OrgName}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章