Silverlight 4 前景色动画

发布于 2024-09-15 17:05:07 字数 540 浏览 4 评论 0原文

当用户将鼠标悬停在控件上时,我尝试对超链接按钮的前景色进行动画处理。我创建了一个自定义样式,我想在其中对前景色进行动画处理。前景色设置如下

<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 控件中设置

I am trying to animate the Foreground color of a hyperlinkbutton when the user MouseOver the control. I created a custom style in which I want to animate the Foreground color. The Foreground color is set like so

<Setter Property="Foreground" Value="#FF73A9D8"/>

In the visualStateManager section I have the following element for the Color Animation

<ColorAnimation Duration="0:0:0.5" 
              Storyboard.TargetName="contentPresenter" 
              Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" 
              To="Black" />

The problem is that I can not figure out what the value should be Storyboard.TargetName.

The text is set in a ContentPresenter control which does not have a Foreground property

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-09-22 17:05:07

你是对的。控件模板内没有地方可以悬挂动画。

虽然 HyperlinkBut​​ton 有一个前景属性,该属性由其内容继承,但该属性不会作为模板的一部分公开。

最好的选择是创建一个通过 MouseEnter/MouseLeave 行为播放 2 个故事板的用户控件(下面的“GlowingHyperlinkBut​​ton”XAML)。当然,您仍然需要通过依赖属性公开内容:

<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>

对可怕的颜色选择表示歉意。希望这有帮助:)

You are correct. There is nowhere to hang the animation inside the control template.

While the HyperlinkButton has a foreground property, which is inherited by its content, the property is not exposed as part of the template.

Your best bet is to create a usercontrol that plays 2 storyboards via MouseEnter/MouseLeave behaviours ("GlowingHyperlinkButton" XAML below). You will of course still need to expose the content via a dependency property:

<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>

Apologies for the horrid choice of colours. Hope this helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文