WPF设置样式的几种方式

WPF设置样式的几种方式

第一种方式是直接使用Setter来进行,可以对Background等进行设置。

<Window.Resources>
   <Style TargetType="Button">
      <Setter Property="Background" Value="Red"/>
   </Style>
</Window.Resources>

第二种是直接将比较复杂一点的Style放置到Window.Resources中:

<Window.Resources>
<LinearGradientBrush x:Key="CheckedState" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFE7E1E1" Offset="0"/>
        <GradientStop Color="black" Offset="1"/>
        <GradientStop Color="gray" Offset="0.581"/>
    </LinearGradientBrush>
</Window.Resources>

使用的时候,直接利用 Background=”{StaticResource CheckedState}”即可。

第三种是利用Template方式来进行,这种可以进行比较复杂的样式制作:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication4.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480">
<Window.Resources>
    <LinearGradientBrush x:Key="CheckedState" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFE7E1E1" Offset="0"/>
        <GradientStop Color="black" Offset="1"/>
        <GradientStop Color="gray" Offset="0.581"/>
    </LinearGradientBrush>

    <LinearGradientBrush x:Key="UnCheckedState" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFE7E1E1" Offset="0"/>
        <GradientStop Color="gray" Offset="1"/>
        <GradientStop Color="gray" Offset="0.581"/>
    </LinearGradientBrush>

    <Style TargetType="TabItem">
        <Setter Property="Template">
            <Setter.Value>
                  <ControlTemplate TargetType="TabItem">
                          <Grid>
                              <Border Name="Border" BorderThickness="1" BorderBrush="Gray" Background="{StaticResource UnCheckedState}"  Width="80" Height="25" Margin="0,0,1,0" CornerRadius="4,4,0,0">
                                <Grid>
                                     <ContentPresenter x:Name="ContentSite"
                                              VerticalAlignment="Center"
                                              HorizontalAlignment="Center"
                                              ContentSource="Header"
                                              Margin="12,2,12,2"
                                              RecognizesAccessKey="True" />
                                </Grid>    
                            </Border>     
                        </Grid>
                        
                         <ControlTemplate.Triggers>
                          <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Panel.ZIndex" Value="100" />
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource CheckedState}" />
                            <Setter TargetName="Border" Property="BorderThickness" Value="1,1,1,0" />
                          </Trigger>
                          <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
                          </Trigger>
                        </ControlTemplate.Triggers>
                        
                    </ControlTemplate>
                    
                    
            </Setter.Value>
        </Setter>
    </Style>

</Window.Resources>
    
    <Grid x:Name="LayoutRoot">
        <TabControl Margin="0,0,0,116">
            <TabControl.Resources>
                <Style TargetType="TabPanel">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                </Style>
            </TabControl.Resources>
            <TabItem Header="TabItem" >TabItem</TabItem>
            <TabItem Header="My Item">My Item</TabItem>
            <TabItem Header="My Database">My Database</TabItem>
            <TabItem Header="TabItem"></TabItem>
            <TabItem Header="TabItem"></TabItem>
            <TabItem Header="TabItem"></TabItem>
        </TabControl>
    </Grid>
</Window>

当然,制作出来的效果也就是好看一些,显示效果如下:

WPF设置样式的几种方式