Silverlight 按钮MouseOver状态下 文字、图片、背景同时改变

Button 当MouseOver时,其布局中的文字改变颜色,图片换图,背景色改变
例如:以下两张图片分别代表正常状态与鼠标滑过状态

 

按钮代码如下:

<Button Style="{StaticResource btnStyle}" Content="ABCDEFG" />

样式代码如下:

<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Padding" Value="1"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="#FF999900" />
<Setter Property="Foreground" Value="#FF3191c3"/>
<Setter Property="FontSize" Value="16" />
<Setter Property="Width" Value="110" />
<Setter Property="Height" Value="70" />
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
 <Setter.Value>
  <ControlTemplate TargetType="Button">
   <Grid>
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="MouseOver">
          <Storyboard>
            <ColorAnimationUsingKeyFrames Duration="1" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Background">
              <EasingColorKeyFrame KeyTime="0" Value="#FF3191c3" />
            </ColorAnimationUsingKeyFrames>
            <ObjectAnimationUsingKeyFrames BeginTime="0" Storyboard.TargetName="icon" Storyboard.TargetProperty="(Image.Source)">
              <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                  <BitmapImage UriSource="images/ywbl_sel.png" />
                </DiscreteObjectKeyFrame.Value>
              </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <ColorAnimation Duration="0" To="#FFFFFF" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock" />
          </Storyboard>
       </VisualState>
       <VisualState x:Name="Pressed">
          <Storyboard>
        
          </Storyboard>
       </VisualState>
       <VisualState x:Name="Disabled">
          <Storyboard>
            <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="DisabledVisualElement"/>
          </Storyboard>
       </VisualState>
     </VisualStateGroup>
     <VisualStateGroup x:Name="FocusStates">
      <VisualState x:Name="Focused">
          <Storyboard>
        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualElement"/>
          </Storyboard>
      </VisualState>
      <VisualState x:Name="Unfocused"/>
     </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <Border x:Name="Background" Opacity="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
          <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="*" />
          </Grid.RowDefinitions>
          <Image x:Name="icon" Grid.Row="0" Source="images/ywbl.png" Width="25" Height="25" HorizontalAlignment="Center" Margin="0 10 0 0"/>
          <TextBlock x:Name="textBlock" Grid.Row="1" Text="{TemplateBinding Content}" Margin="0 -5 0 0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="Center" />
        </Grid>
    </Border>
    <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" IsHitTestVisible="false" Opacity="0" RadiusY="3" RadiusX="3"/>
    <Rectangle x:Name="FocusVisualElement" IsHitTestVisible="false" Margin="1" Opacity="0" RadiusY="2" RadiusX="2" Stroke="#FF6DBDD1" StrokeThickness="0"/>
   </Grid>
  </ControlTemplate>
 </Setter.Value>
</Setter>
</Style>


=====  记录完毕, 为日后方便在项目使用====

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值