微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐