• WPF ControlTemplate


    ButtonStyle.xaml

    View Code
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <SolidColorBrush  x:Key="whiteSolidBrush" Color="White"/>
        <SolidColorBrush  x:Key="blackSolidBrush" Color="Black"/>
    
        <LinearGradientBrush  x:Key="blackToWhiteLGBrush">
            <GradientStop Offset="0" Color="Black"/>
            <GradientStop Offset="1" Color="White"/>
        </LinearGradientBrush>
    
        <RadialGradientBrush x:Key="blackToWhiteRGBrush">
            <GradientStop Offset="0" Color="Black"/>
            <GradientStop Offset="1" Color="White"/>
        </RadialGradientBrush>
    
        <ControlTemplate x:Key="buttonTemplate" TargetType="Button">
           <Grid>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState Name="Normal"/>
                        <VisualState Name="MouseOver"/>
                        <VisualState Name="Pressed">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                                Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0:0:0"
                                                            Value="{StaticResource whiteSolidBrush}"/>
                                </ObjectAnimationUsingKeyFrames>
                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentControl"
                                                                Storyboard.TargetProperty="Foreground">
                                    <DiscreteObjectKeyFrame KeyTime="0:0:0"                
                                                            Value="{StaticResource blackSolidBrush}"/>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState Name="Disabled">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="disabRectangle"
                                                 Storyboard.TargetProperty="Opacity"
                                                 To="0.6" Duration="0:0:0"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
               
               <Border Name="border"
                       BorderBrush="{TemplateBinding BorderBrush}"
                       Background="{TemplateBinding Background}"
                       BorderThickness="{TemplateBinding BorderThickness}"
                       CornerRadius="5">
                   <ContentControl Name="contentControl"
                                   Content="{TemplateBinding Content}"
                                   ContentTemplate="{TemplateBinding ContentTemplate}"
                                   Margin="{TemplateBinding Padding}"
                                   HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                   VerticalAlignment="{TemplateBinding VerticalAlignment}"/>
               </Border>
                <Rectangle Name="disabRectangle"
                           Fill="Gainsboro"
                           Opacity="0"/> 
            </Grid>
       </ControlTemplate>
        
        <Style x:Key="buttonStyle" TargetType="Button">
            <Setter Property="BorderBrush" Value="White"/>
            <Setter Property="BorderThickness" Value="5"/>
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="Template" Value="{StaticResource buttonTemplate}"/>
        </Style>
    </ResourceDictionary>

    使用

    <Button x:Name="bt" Content="123" Style="{StaticResource buttonStyle}" IsEnabled="False"/>
  • 相关阅读:

    队列
    Collection类
    Hashtable类、IdentityHashMap和WeakHashMap类
    LinkedHashMap类
    广播的种类:有序广播和无序广播
    自定义的BroadCastReceiver
    String的两个API,判断指定字符串是否包含另一字符串,在字符串中删除指定字符串。
    BroadcastReceiver的最简单用法
    Notification通知栏
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/3075755.html
Copyright © 2020-2023  润新知