Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WPF使用故事板向动画添加多个效果_C#_Wpf_Animation - Fatal编程技术网

C# 使用WPF使用故事板向动画添加多个效果

C# 使用WPF使用故事板向动画添加多个效果,c#,wpf,animation,C#,Wpf,Animation,我在WPF中的动画序列中添加多个效果时遇到问题。我在网格中排列了多个矩形,动画效果的工作方式如下所示: 默认情况下,用户看到网格时,每个单元格都以黑色画布上的银色边框为边界,每个单元格内矩形的颜色可以是透明/黑色 鼠标悬停时,单元格中的矩形将其笔划和填充更改为绿色 退出鼠标悬停时,上一个单元格会在鼠标悬停前慢慢将颜色更改为其默认状态 我能够只为笔划颜色做动画效果,但不能将其与填充属性结合。以下是用于在网格中创建矩形的代码片段: Style cellStyle = Prep

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

  • 默认情况下,用户看到网格时,每个单元格都以黑色画布上的银色边框为边界,每个单元格内矩形的颜色可以是透明/黑色
  • 鼠标悬停时,单元格中的矩形将其笔划和填充更改为绿色
  • 退出鼠标悬停时,上一个单元格会在鼠标悬停前慢慢将颜色更改为其默认状态
  • 我能够只为笔划颜色做动画效果,但不能将其与填充属性结合。以下是用于在网格中创建矩形的代码片段:

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