使用WPF使用故事板为动画添加多个效果

时间:2014-07-21 16:01:16

标签: c# wpf animation

我在WPF中的动画序列中添加多个效果时遇到问题。我在网格中排列了多个矩形,动画效果的工作方式如下:

  1. 默认情况下,用户会看到网格,使得每个单元格都被黑色画布上的银色边框限制,每个单元格中矩形的颜色可以是透明/黑色。
  2. 在鼠标悬停时,单元格中的矩形会更改其笔触并填充为绿色。
  3. 退出鼠标时,前一个单元格会在鼠标悬停前缓慢将颜色更改为默认状态。
  4. 我能够仅针对笔触颜色执行动画效果,但不能将其与填充属性结合使用。以下是在网格中创建矩形的代码段:

                Style cellStyle = PrepareAnimationStyle();
                foreach (string label in rowHeaders)
                {
                    for (int n = 0; n < numOfCols; n++)
                        grid.Children.Add(new Rectangle()
                        {
                            Stroke = Brushes.Silver,
                            StrokeThickness = 2,
                            Fill = Brushes.Transparent,
                            Height = cellSize,
                            Width = cellSize,
                            Style = cellstyle
                        });
                }
    

    以下是设置动画的代码(仍在进行中,无法使其按要求运行):

    Style PrepareAnimationStyle()
    {
        Trigger animTrigger = new Trigger();
        animTrigger.Property = ContentElement.IsMouseOverProperty;
        animTrigger.Value = true;
    
        System.Windows.Media.Animation.ColorAnimation toGreen = new          System.Windows.Media.Animation.ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0));
        toGreen.FillBehavior = FillBehavior.HoldEnd;
        System.Windows.Media.Animation.ColorAnimation toTransparent = new System.Windows.Media.Animation.ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1));
        System.Windows.Media.Animation.ColorAnimation toSilver = 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(toGreen, new PropertyPath("Stroke.Color"));
        Storyboard.SetTargetProperty(toGreen, new PropertyPath("(Shape.Fill).(SolidColorBrush.Color)"));
        sbEnter.Children.Add(toGreen);
    
        /*Storyboard sbFill = new Storyboard();
        Storyboard.SetTargetProperty(toGreen, new PropertyPath("Fill.Color"));
        sbFill.Children.Add(toSilver);
    
        Storyboard sbFillEmpty = new Storyboard();
        Storyboard.SetTargetProperty(toTransparent, new PropertyPath("Fill.Color"));
        sbFillEmpty.Children.Add(toSilver);*/
    
        Storyboard sbExit = new Storyboard();
        //Storyboard.SetTargetProperty(toSilver, new PropertyPath("Stroke.Color"));
        Storyboard.SetTargetProperty(toTransparent, new PropertyPath("Fill.Color"));
        sbExit.Children.Add(toSilver);
    
        animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter });
        //animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbFill });
        //animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbFillEmpty });
        animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit });
    
        Style cellStyle = new Style();
        cellStyle.Triggers.Add(animTrigger);
    
        return cellStyle;
    }
    

1 个答案:

答案 0 :(得分:2)

你去吧

    Style PrepareAnimationStyle()
    {
        Trigger animTrigger = new Trigger();
        animTrigger.Property = FrameworkElement.IsMouseOverProperty;
        animTrigger.Value = true;

        ColorAnimation strokeToGreen = new ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0));
        ColorAnimation strokeToSilver = new ColorAnimation(Colors.Silver, TimeSpan.FromSeconds(1));

        ColorAnimation fillToGreen = new ColorAnimation((Color)ColorConverter.ConvertFromString("#FF66CC00"), TimeSpan.FromSeconds(0));
        ColorAnimation fillToTransparent = new ColorAnimation(Colors.Transparent, TimeSpan.FromSeconds(1));

        Storyboard sbEnter = new Storyboard();
        Storyboard.SetTargetProperty(strokeToGreen, new PropertyPath("Stroke.Color"));
        Storyboard.SetTargetProperty(fillToGreen, new PropertyPath("Fill.Color"));
        sbEnter.Children.Add(strokeToGreen);
        sbEnter.Children.Add(fillToGreen);

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

        animTrigger.EnterActions.Add(new BeginStoryboard() { Storyboard = sbEnter });
        animTrigger.ExitActions.Add(new BeginStoryboard() { Storyboard = sbExit });

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

        return cellStyle;
    }

为了使其正常工作,请确保在添加单元格时设置填充和描边

例如

        Style cellStyle = PrepareAnimationStyle(); //this line is out side of the cell loop
        ....
        rect.Fill = new SolidColorBrush(Colors.Transparent);
        rect.Stroke = new SolidColorBrush(Colors.Silver);
        rect.Style = cellStyle;