【问题标题】:Adding multiple effects to animation using storyboard using WPF使用 WPF 使用情节提要向动画添加多种效果
【发布时间】:2014-09-12 05:15:04
【问题描述】:

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

  1. 默认情况下,用户看到网格时,每个单元格都以黑色画布上的银色边框为界,每个单元格内的矩形颜色可以是透明/黑色。
  2. 鼠标悬停时,单元格中的矩形将其笔触和填充更改为绿色。
  3. 退出鼠标悬停时,前一个单元格会慢慢将颜色变为鼠标悬停之前的默认状态。

我能够只为描边颜色制作动画效果,但不能将其与填充属性结合使用。这是在网格中创建矩形的代码 sn-p:

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

【问题讨论】:

    标签: c# wpf animation


    【解决方案1】:

    给你

        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;
    

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 2015-04-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2010-10-16
      • 2012-05-23
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多