WPF 以编程方式创建事件触发器

WPF create event trigger programmatically

我有一个 canvas,我可以在其中以编程方式添加更多形状。对于其中一个形状(Path),我想添加一种每秒都会闪烁的填充颜色(从红色变为蓝色然后再变为蓝色)。 我从 xaml:

找到了一个关于如何做到这一点的例子
<Ellipse Fill="Red">
<Ellipse.Triggers>
    <EventTrigger RoutedEvent="Ellipse.Loaded">
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Ellipse.Fill).(SolidColorBrush.Color)"
                                                  Duration="0:0:2"
                                                  FillBehavior="HoldEnd"
                                                  RepeatBehavior="Forever">
                        <ColorAnimationUsingKeyFrames.KeyFrames>
                            <DiscreteColorKeyFrame KeyTime="0:0:0" Value="Red"/>
                            <DiscreteColorKeyFrame KeyTime="0:0:1" Value="Blue"/>
                        </ColorAnimationUsingKeyFrames.KeyFrames>
                    </ColorAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>                    
    </EventTrigger>
</Ellipse.Triggers>

但是当我通过代码执行此操作时,我收到 ArgumentNullException: "{"Value cannot be null.\r\nParameter name: routedEvent"}"

这是我的代码:

 var sheetPath = new Path
        {
            Stroke = Brushes.Black,
            Fill = !isSelectedSheet ? Brushes.MediumSlateBlue : GetInvertedColor(Brushes.MediumSlateBlue),
            StrokeThickness = _lineWidth,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Center,
            Data = CreatePathGeometry(contour, height)
        };

        var colorAnimationUsingKeyFrames = new ColorAnimationUsingKeyFrames
        {
            Duration = new Duration(new TimeSpan(0, 0, 0, 300)),
            RepeatBehavior = RepeatBehavior.Forever,
            FillBehavior = FillBehavior.HoldEnd
        };

        colorAnimationUsingKeyFrames.KeyFrames.Add(new DiscreteColorKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 100)),
            Value = Colors.Red
        });
        colorAnimationUsingKeyFrames.KeyFrames.Add(new DiscreteColorKeyFrame
        {
            KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, 200)),
            Value = Colors.Blue
        });

        var storyboard = new Storyboard();
        storyboard.Children.Add(colorAnimationUsingKeyFrames);

        Storyboard.SetTargetProperty(storyboard.Children[0], new PropertyPath("(Path.Fill).(SolidColorBrush.Color)"));
        var beginStoryboard = new BeginStoryboard();

        beginStoryboard.Storyboard = storyboard;

        var eventTrigger = new EventTrigger();
        eventTrigger.Actions.Add(beginStoryboard);

        sheetPath.Triggers.Add(eventTrigger);

        canvas.Children.Add(sheetPath);

设置 RoutedEvent 属性 的 EventTrigger:

var eventTrigger = new EventTrigger();
eventTrigger.RoutedEvent = LoadedEvent;
eventTrigger.Actions.Add(beginStoryboard);