【全面解禁!真正的Expression Blend实战开发技巧】第三章 从最常用ButtonStyle开始 - TextButton...

原文: 【全面解禁!真正的Expression Blend实战开发技巧】第三章 从最常用ButtonStyle开始 - TextButton

  在实际项目中,使用blend做的最多的一定是各种自定义Button。每位开发者都不可避免。而在Button的各种样式中,使用率最高的一定是TextButton与ImageButton这两种按钮。

     本章以TextButton为例,讲解如何最简单,最快速的制作一个专业的TextButton。 对于这个TextButton的需求是这样的,鼠标悬浮时,光标变为“手指”,并改变按钮的前景色,鼠标按下时,有明显的按下的感觉。许多朋友都会遇到一个问题,ContentPresenter没有Foreground属性,如何改变前景色?我在这里介绍一种最快速最简单的办法。

     新建一个TextBlock,输入I'm TextButton

     img_909110bc98b59f614cdc6e012788ef70.jpe

  右击TextBolck,选择Make Into Control

    img_ff47a343f1699e2b3f160ea0157a2493.jpe

  为我们的样式取名为TextButtonStyle,Blend默认每次新建样式时的ControlType就是Button,所以直接点击OK

    img_86244d20a03b816c637ca09391bef5c1.jpe

   看一下Blend为我们生成的元素列表,他将TextBlock自动转换为ContentPresenter,并用一个Grid包裹。

    img_2830ea5613edca679b14a6c1524551dd.jpe

  鼠标悬浮时,改变按钮的前景颜色,为了实现这个效果,先将ContentPresenter替换为ContentControl 。(替换方法为:先删除ContentPresenter,然后点击工具条上的img_f4a7ab523d514cce0a2fc218a25176ce.jpe按钮,在搜索栏中输入ContentControl,如果没结果请稍等几十秒s)   

    img_9b320e5039220d361ca7fdeb518d8e59.jpe

  将ContentControl.Content属性与Button逻辑树中的Content属性绑定。点击ContentControl,在右侧属性面板中,找到Content属性,点击Content属性最右侧的小方块。    

    img_9a334e842ba0d87f28cff8435fb511a1.jpe

   在弹出菜单中,顺序选择Template Binding  -> Content    

    img_583b3199c864d2630c7c0c8d8109116a.jpe

  如果你上面的步骤都操作正确的话,ContentControl的Content属性会自动绑定我们之前输入的文本    

    img_d6239412e17893774f5463f3cce82aa3.jpe

   为什么要使用ContentControl?原因在于ContentControl比ContentPresenter多了一个Foreground属性。这样我们可以方便的改变按钮的前景色,无论他是文本还是Path。下面我们开始制作OnMouseOver时的动画,尝试改变前景色。打开States面板,选择MouseOver,点击显示时间线13图标,将黄色时间线拖拽到0.3秒处。                     

     img_f89e1c2a09a8856e1f6f6fa48bedbd9a.jpe

  设置前景色为#FFDE9107               

     img_ae9e437a4d462bb49c3b29d9feb51faf.jpe

  点击停止录制按钮,暂时暂停动画的录制。               

       img_65084396bc5b7ad500d2889a6f942a54.jpe

  设置Cursor为hand,然后点击开始录制按钮,启动动画录制。               

      img_31aaf34434d1ba485d21e396e9078875.jpe

  此时MouseOver状态下动画面板应该是这样的              

      img_14821266bf6ca218867f07986b3222ec.jpe

     

  接下来我们定义压下效果,首先右击States面板中的MouseOver,选择Copt State To然后选择Pressed               

               img_794cdba0708cc5b35aea2cea704e3327.jpe

  

  点击Preesed状态,在对象面板中选中ContentControl                

           img_4d17e4d902662cbb6a1f34c7cca45ae2.jpe

  设置ContentControl的RenderTransform.TranslateX 为1 ,RenderTransform.TranslateY为1               

               img_75a1e4f7d06eae2358531b545a5a4bdd.jpe

  

  此时Pressed状态下动画面板应该是这样的               

               img_5f20ba03e15d62f6e4aab4fce304b900.jpe

  

 xaml代码如下:
<Style x:Key="TextButtonStyle" TargetType="Button">
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="Button">
      <Grid>
       <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
         <VisualStateGroup.Transitions>
          <VisualTransition GeneratedDuration="0"/>
         </VisualStateGroup.Transitions>
         <VisualState x:Name="Normal"/>
         <VisualState x:Name="MouseOver">
          <Storyboard>
           <ColorAnimation Duration="0:0:0.3" To="#FFDE9107"
           Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
           Storyboard.TargetName="contentControl" d:IsOptimized="True"/>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="Pressed">
          <Storyboard>
           <ColorAnimation Duration="0:0:0.3" To="#FFDE9107"
           Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
           Storyboard.TargetName="contentControl" d:IsOptimized="True"/>
           <DoubleAnimation Duration="0:0:0.3" To="1"
           Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateX)"
           Storyboard.TargetName="contentControl" d:IsOptimized="True"/>
           <DoubleAnimation Duration="0:0:0.3" To="1"
           Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
            Storyboard.TargetName="contentControl" d:IsOptimized="True"/>
          </Storyboard>
         </VisualState>
         <VisualState x:Name="Disabled"/>
        </VisualStateGroup>
       </VisualStateManager.VisualStateGroups>
       <ContentControl x:Name="contentControl" Content="{TemplateBinding Content}"
        d:LayoutOverrides="Width, Height" Cursor="Hand" RenderTransformOrigin="0.5,0.5">
        <ContentControl.RenderTransform>
         <CompositeTransform/>
        </ContentControl.RenderTransform>
       </ContentControl>
      </Grid>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
   <Setter Property="FontFamily" Value="28 Days Later"/>
  </Style>
 
 我用了一个名为28 Days Later的字体,如果你没有,可以随意设置其他的字体。 
 
  
< Setter Property ="FontFamily" Value ="28 Days Later" />

TextButton在实际项目中使用率非常高。几乎90%的项目都会使用TextButton,按照本文的方法,你可以快速,简单的定制TextButton,希望本文对大家有帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值