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>
    复制代码

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

  • 相关阅读:
    使用 console.time() 计算js代码执行时间
    javascript 如何创建只能执行一次的事件。
    Javascript 的addEventListener()及attachEvent()对比
    使用jasmine-node 进行NodeJs单元测试 环境搭建
    Karma和Jasmine 自动化单元测试环境搭建
    3487. 【NOIP2013模拟联考11】剑与魔法(dragons) (Standard IO)
    3470. 【NOIP2013模拟联考8】最短路(path) (Standard IO)
    2018洛谷8月月赛第一题_U28036 Nagisa loves Tomoya
    NOIP2017提高组Day2第一题
    3464. 【NOIP2013模拟联考6】秀姿势(sugata) (Standard IO)
  • 原文地址:https://www.cnblogs.com/fuchongjundream/p/3864772.html
Copyright © 2011-2022 走看看