WPF将Run元素设置为Flash元素

时间:2011-03-03 17:52:02

标签: wpf animation

这是我现在遇到的一个奇怪的问题。我想要做的是将Run元素设置为基本上闪烁/闪烁。父级是一个超链接,它包含多个类型为Run和Image的Inlines。现在我试图为元素的前景颜色设置动画,但它似乎不起作用。

这是我的超链接代码。

 CallbackHyperLink callbackLink = new CallbackHyperLink();
 ToolTipService.SetShowDuration(callbackLink, 3600000);
 ToolTipService.SetInitialShowDelay(callbackLink, 0);
 callbackLink.Foreground = new SolidColorBrush(Colors.Magenta);                        // Default text color of the link

 callbackLink.TextDecorations = null;                            // Disable the underline until mouse over
 callbackLink.ToolTip = f.Tooltip;                               // Set the tooltip string

DoubleAnimation opacityAnim = new DoubleAnimation();
opacityAnim.From = 1.0;
opacityAnim.To = 0.0;
opacityAnim.FillBehavior = FillBehavior.Stop;
opacityAnim.Duration = TimeSpan.FromSeconds(BlinkDurationOff);
opacityAnim.AutoReverse = true;
_blinkAnimation.Children.Add(opacityAnim);

Storyboard.SetTarget(opacityAnim, callbackLink.Foreground);
Storyboard.SetTargetProperty(opacityAnim, new PropertyPath(SolidColorBrush.OpacityProperty));

_blinkAnimation.Stop();
_blinkAnimation.Begin();

因此被放入一个被解雇的故事板。然而前景并没有动画,我没有看到任何警告我试图动画我不应该的东西。有人有什么想法吗?

由于

2 个答案:

答案 0 :(得分:0)

这有效:

Storyboard.SetTarget(opacityAnim, callbackLink); 
Storyboard.SetTargetProperty(opacityAnim, new PropertyPath(UIElement.OpacityProperty));
使用TextBlock stolen from here

修改完整的工作示例

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    TextBlock callbackLink = new TextBlock();
    callbackLink.HorizontalAlignment = HorizontalAlignment.Center;
    callbackLink.VerticalAlignment = VerticalAlignment.Center;
    callbackLink.Text = "Test";
    this.Content = callbackLink;

    NameScope.SetNameScope(this, new NameScope());

    var b = new SolidColorBrush(Colors.Magenta);
    callbackLink.Foreground = b;
    this.RegisterName("MyAnimatedBrush", b);

    DoubleAnimation opacityAnimation = new DoubleAnimation();
    opacityAnimation.To = 0.0;
    opacityAnimation.Duration = TimeSpan.FromSeconds(0.5);
    opacityAnimation.AutoReverse = true;
    opacityAnimation.RepeatBehavior = RepeatBehavior.Forever;
    Storyboard.SetTargetName(opacityAnimation, "MyAnimatedBrush");
    Storyboard.SetTargetProperty(
        opacityAnimation, new PropertyPath(SolidColorBrush.OpacityProperty));
    Storyboard mouseLeftButtonDownStoryboard = new Storyboard();
    mouseLeftButtonDownStoryboard.Children.Add(opacityAnimation);
    callbackLink.MouseEnter += delegate(object sender2, MouseEventArgs ee)
    {
        mouseLeftButtonDownStoryboard.Begin(this, true);
    };

    callbackLink.MouseLeave += delegate(object sender2, MouseEventArgs ee)
    {
        mouseLeftButtonDownStoryboard.Stop(this);
    };


}

答案 1 :(得分:0)

另一种链接到前景不透明度调用而不注册画笔名称的方法如下:

    Storyboard.SetTarget(opacityAnim, callbackLink);
    Storyboard.SetTargetProperty(opacityAnim, new PropertyPath("Foreground.Opacity"));

这似乎终于对我有用了。仍在调整我的解决方案,因为我在动画上做了很多停止/开始调用,我认为这不是一种好的做事方式。