4

有没有办法在 opacity 属性上为装饰器设置动画,使线条从微红色变为全红色>

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

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

谢谢

4

1 回答 1

7

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

窗口的 XAML:

<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));
        }

    }
}
于 2012-06-07T06:52:40.630 回答