简体   繁体   English

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

[英]Animating a line adorner in wpf?

Is there a way to animate an adorner on the opacity property so that the line appears from a slight reddish color to a full red color> 有没有办法在不透明度属性上设置装饰器的动画,以便该线从浅红色到全红色显示>

I have the following code in my OnRender() method: 我的OnRender()方法中有以下代码:

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

Thank you 谢谢

Here is an example; 这是一个例子; press the button and the red line will fade in and out. 按下按钮,红线将淡入淡出。

XAML for window: 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>

Code-behind for 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);
        }
    }
}

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

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM