繁体   English   中英

使用WPF使用情节提要向动画添加多种效果

[英]Adding multiple effects to animation using storyboard using WPF

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

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

我能够仅对笔触颜色执行动画效果,但无法将其与填充属性结合使用。 这是在网格内创建矩形的代码片段:

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

干得好

    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;

暂无
暂无

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

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