C# 如何为WPF形状的动画样式添加增量计数器

C# 如何为WPF形状的动画样式添加增量计数器,c#,wpf,animation,styles,C#,Wpf,Animation,Styles,我需要添加一些类似计数器的功能,检测形状(矩形)悬停了多少次,并相应地将动画样式更改一点 背景:我有一个网格单元格(矩形),每个单元格都有鼠标悬停事件。有两种类型的细胞;单元格类型1是普通单元格,在鼠标悬停时必须变为绿色,在鼠标悬停结束时必须恢复为原始的透明颜色。类型2单元格是特殊的,当鼠标悬停时必须变为绿色,即使鼠标离开矩形也必须保持绿色 正在使用的代码: Style PrepareAnimationStyle(int cellType) { Trigg

我需要添加一些类似计数器的功能,检测形状(矩形)悬停了多少次,并相应地将动画样式更改一点

背景:我有一个网格单元格(矩形),每个单元格都有鼠标悬停事件。有两种类型的细胞;单元格类型1是普通单元格,在鼠标悬停时必须变为绿色,在鼠标悬停结束时必须恢复为原始的透明颜色。类型2单元格是特殊的,当鼠标悬停时必须变为绿色,即使鼠标离开矩形也必须保持绿色

正在使用的代码:

Style PrepareAnimationStyle(int cellType)
        {
            Trigger animTrigger = new Trigger();
            animTrigger.Property = ContentElement.IsMouseOverProperty; 

            System.Windows.Media.Animation.ColorAnimation greenStroke = new System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0));
            //greenStroke.FillBehavior = FillBehavior.HoldEnd;
            System.Windows.Media.Animation.ColorAnimation greenFill = new System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0));
            //greenFill.FillBehavior = FillBehavior.HoldEnd;

            System.Windows.Media.Animation.ColorAnimation transparentFill = new System.Windows.Media.Animation.ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1));
            System.Windows.Media.Animation.ColorAnimation silverStroke = new System.Windows.Media.Animation.ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1));

            System.Windows.Media.Animation.Storyboard sbEnter = new System.Windows.Media.Animation.Storyboard();
            Storyboard.SetTargetProperty(greenStroke, new PropertyPath("Stroke.Color"));
            Storyboard.SetTargetProperty(greenFill, new PropertyPath("Fill.Color"));
            sbEnter.Children.Add(greenStroke);
            sbEnter.Children.Add(greenFill);

            Storyboard sbExit = new Storyboard();
            Storyboard.SetTargetProperty(silverStroke, new PropertyPath("Stroke.Color"));
            Storyboard.SetTargetProperty(transparentFill, new PropertyPath("Fill.Color"));
            sbExit.Children.Add(silverStroke);
            sbExit.Children.Add(transparentFill);

           animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter });
            if (cellType != 2) //regular cells
                animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit });

            Style cellStyle = new Style();
            cellStyle.Triggers.Add(animTrigger);

            return cellStyle;
        }
问题:如果没有悬停转弯次数的条件,此代码可以正常工作。现在,我很难理解如何引入一个计数器,可以在特殊单元格悬停时改变其阴影/不透明度。我可以制作三种不同的色调-浅绿色、中绿色、深绿色并使用它们,或者我可以使用不透明度变量并逐渐增加它;但我不明白如何使用现有代码来检查此计数器的值,以及如何应用必要的输入动画样式。这就是我创建单元格的方式:

grid.Children.Add(new Rectangle()
            {
                Stroke = Brushes.Silver,
                StrokeThickness = 2,
                Fill = Brushes.Transparent,
                Height = cellSize,
                Width = cellSize,
                Style = PrepareAnimationStyle(cellType)
            }); 

有人能帮我弄清楚吗?谢谢

我用附加属性解决了这个问题

让我们从rect生成开始

    Rectangle rect = new Rectangle()
    {
        Stroke = new SolidColorBrush(Colors.Silver),
        StrokeThickness = 2,
        Fill = new SolidColorBrush(Colors.Transparent),
        Height = cellSize,
        Width = cellSize,
        Style = (cellType != 2) ? PrepareAnimationStyle(cellType) : null
    };
    if (cellType == 2)
        rect.SetValue(AnimationHelper.IsNonRegularCellProperty, true);
    grid.Children.Add(rect);
请注意,将颜色值初始化为
新SolidColorBrush(Colors…
对于工作方法很重要,在这种情况下,非规则单元格也不需要样式

我们将附加一个值为true的属性
AnimationHelper.IsNonRegularCellProperty
,以启用自定义动画

AnimationHelper类

    class AnimationHelper : DependencyObject
    {
        // Using a DependencyProperty as the backing store for IsNonRegularCell.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsNonRegularCellProperty =
            DependencyProperty.RegisterAttached("IsNonRegularCell", typeof(bool), typeof(AnimationHelper), new PropertyMetadata(false, OnIsNonRegularCellChanged));

        private static void OnIsNonRegularCellChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Rectangle rect = d as Rectangle;
            if ((bool)e.NewValue)
            {
                //to make sure I set the color values again
                rect.Stroke = new SolidColorBrush(Colors.Silver);
                rect.Fill = new SolidColorBrush(Colors.Transparent);
                rect.SetValue(HoverCountProperty, default(byte));
                rect.MouseEnter += rect_MouseEnter;
            }
            else
            {
                rect.MouseEnter -= rect_MouseEnter;
            }
        }

        static void rect_MouseEnter(object sender, MouseEventArgs e)
        {
            Rectangle rect = sender as Rectangle;

            byte hoverCount = (byte)rect.GetValue(HoverCountProperty);
            hoverCount++;
            if (hoverCount > 3)
                return;
            rect.SetValue(HoverCountProperty, hoverCount);
            byte alpha = (byte)(85 * hoverCount);
            ColorAnimation anim = new System.Windows.Media.Animation.ColorAnimation(Color.FromArgb(alpha, 0x66, 0xcc, 0), TimeSpan.FromSeconds(0));
            rect.Fill.BeginAnimation(SolidColorBrush.ColorProperty, anim);
            rect.Stroke.BeginAnimation(SolidColorBrush.ColorProperty, anim);
        }

        // Using a DependencyProperty as the backing store for HoverCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HoverCountProperty =
            DependencyProperty.RegisterAttached("HoverCount", typeof(byte), typeof(AnimationHelper), new PropertyMetadata(default(byte)));
    }

上面的动画将以每次增加33%的alpha值为颜色设置动画,以便在3次鼠标悬停中达到100%。

效果很好!非常感谢你!太好了!快乐编码:)