WPF自定义样式篇

时间:2022-05-02 09:05:05

WPF自定义样式篇

样式:

 <!--DataGrid样式-->
    <Style TargetType="DataGrid">
        <Setter Property="RowHeaderWidth" Value="0"></Setter>
        <Setter Property="AutoGenerateColumns" Value="False"></Setter>
        <Setter Property="CanUserAddRows" Value="False"></Setter>
        <Setter Property="CanUserResizeColumns" Value="False"></Setter>
        <Setter Property="CanUserResizeRows" Value="False"></Setter>
        <Setter Property="HorizontalGridLinesBrush" Value="LightGray"></Setter>
        <Setter Property="VerticalGridLinesBrush" Value="LightGray"></Setter>
        <Setter Property="IsReadOnly" Value="True"></Setter>
        <Setter Property="BorderThickness" Value="1,0"></Setter>
        <Setter Property="BorderBrush" Value="LightGray"></Setter>
        <Setter Property="RowHeight" Value="30"></Setter>
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"></Setter>
    </Style>

 

    <!--DataGrid表头样式-->
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="FontSize" Value="16"></Setter>
        <Setter Property="Background" Value="{StaticResource BgColor}"></Setter>
        <Setter Property="BorderThickness" Value="0,0,1,1"></Setter>
        <Setter Property="BorderBrush" Value="LightGray"></Setter>
        <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
    </Style>

 

<!--DataGrid复选框样式-->

 <Style TargetType="CheckBox" x:Key="VerticalCheckBox">
        <Setter Property="VerticalAlignment" Value="Center"></Setter>
        <Setter Property="FontSize" Value="16"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <StackPanel HorizontalAlignment="Center" >
                        <ContentPresenter HorizontalAlignment="Center" Margin="2"></ContentPresenter>
                        <Border x:Name="bd" BorderThickness="1.5" BorderBrush="Gray" >
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0"  EndPoint="1,1">
                                    <GradientStop Color="LightGray" Offset="0.05"/>
                                    <GradientStop Color="White" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                            <Path Stroke="Black"  StrokeThickness="2"></Path>
                        </Border>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter TargetName="checkPath" Property="Data" Value="M 1.5,5 L 7,13 17,0"></Setter>