zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    浅谈ASP.NET内部机制(四)
    用正则表达式看.NET编程正则核心对象详解(三 )
    【讨论】对技术的掌握到底应该又多深?
    掌握XML系列(三)创建格式良好的饿XML文档 续编
    浅谈ASP.NET的内部机制(一)
    浅谈ASP.NET内部机制(三)
    浅谈ASP.NET的内部机制(二)
    小工具大智慧
    老生常谈:抽象工厂模式
    又说:程序员的成长过程
  • 原文地址:https://www.cnblogs.com/wzwyc/p/6292051.html
Copyright © 2011-2022 走看看