作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在用户将鼠标悬停在控件上时为超链接按钮的前景色设置动画。我创建了一个自定义样式,我想在其中为前景色设置动画。前景色是这样设置的
<Setter Property="Foreground" Value="#FF73A9D8"/>
在 visualStateManager 部分,我有以下颜色动画元素
<ColorAnimation Duration="0:0:0.5"
Storyboard.TargetName="contentPresenter"
Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
To="Black" />
问题是我不知道 Storyboard.TargetName 应该是什么值。
文本设置在没有 Foreground 属性的 ContentPresenter 控件中
最佳答案
你是对的。控件模板内没有地方可以挂动画。
虽然 HyperlinkButton 有一个前景属性,该属性由其内容继承,但该属性不会作为模板的一部分公开。
最好的办法是创建一个用户控件,通过 MouseEnter/MouseLeave 行为(下面的“GlowingHyperlinkButton”XAML)播放 2 个 Storyboard。当然,您仍然需要通过依赖属性公开内容:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
mc:Ignorable="d"
x:Class="SilverlightApplication1.GlowingHyperlinkButton"
d:DesignWidth="94" d:DesignHeight="16">
<UserControl.Resources>
<Storyboard x:Name="MouseEnterStoryboard">
<ColorAnimation Duration="0:0:0.5" To="#FFDF00EB" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="hyperlinkButton" d:IsOptimized="True"/>
</Storyboard>
<Storyboard x:Name="MouseLeaveStoryboard">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="hyperlinkButton">
<SplineColorKeyFrame KeyTime="0:0:0.5" Value="#FF49ED28"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<HyperlinkButton x:Name="hyperlinkButton" Content="HyperlinkButton" Foreground="#FF49ED28" d:LayoutOverrides="Width, Height">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ControlStoryboardAction Storyboard="{StaticResource MouseEnterStoryboard}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeaveStoryboard}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
</Grid>
</UserControl>
为可怕的颜色选择道歉。希望这会有所帮助:)
关于Silverlight 4 前景色动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3544868/
我是一名优秀的程序员,十分优秀!