在wpf中设置线条装饰器的动画效果?

时间:2012-06-07 01:59:10

标签: c# wpf

是否有办法在不透明度属性上为装饰器设置动画,以使线条从略带红色的颜色显示为完整的红色>

我的OnRender()方法中有以下代码:

Pen renderPen = new Pen(new SolidColorBrush(Colors.Red), 2.5);
drawingContext.DrawLine(renderPen, adornedElementRect.Value.TopLeft, adornedElementRect.Value.BottomLeft);

谢谢

1 个答案:

答案 0 :(得分:5)

这是一个例子;按下按钮,红线将淡入和淡出。

XAML for window:

<Window x:Class="AnimAdornerTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Width="100" Height="30" 
                HorizontalAlignment="Left" 
                VerticalAlignment="Top" Click="Button_Click">Show Adorner</Button>
        <Rectangle x:Name="toBeAdorned" Fill="#E8E8E8" Width="100" Height="100"></Rectangle>
    </Grid>
</Window>

窗口代码隐藏:

using System.Windows;
using System.Windows.Documents;

namespace AnimAdornerTest
{
    public partial class MainWindow : Window
    {
        private AdornerLayer _adornerLayer;
        private AnimAdorner _adorner;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_adornerLayer == null)
            {
                _adornerLayer = AdornerLayer.GetAdornerLayer(toBeAdorned);
            }
            if (_adorner != null)
            {
                _adornerLayer.Remove(_adorner);
            }
            _adorner = new AnimAdorner(toBeAdorned);
            _adornerLayer.Add(_adorner);
        }
    }
}

装饰器:

using System;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace AnimAdornerTest
{
    public class AnimAdorner : Adorner
    {
        public AnimAdorner(UIElement adornedElement) : base(adornedElement)
        {
            Loaded += AnimAdorner_Loaded;
        }

        void AnimAdorner_Loaded(object sender, RoutedEventArgs e)
        {
            DoubleAnimation myDoubleAnimation = new DoubleAnimation
                {
                    From = 1.0,
                    To = 0.0,
                    Duration = new Duration(TimeSpan.FromSeconds(1)),
                    AutoReverse = true,
                    RepeatBehavior = RepeatBehavior.Forever
                };

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTarget(myStoryboard, this);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(OpacityProperty));

            myStoryboard.Begin(this);
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Pen renderPen = new Pen(new SolidColorBrush(Colors.Red), 2.5);
            drawingContext.DrawLine(renderPen, new Point(0, 0), new Point(AdornedElement.RenderSize.Width, AdornedElement.RenderSize.Height));
        }

    }
}