Search code examples
xamlstoryboardeventtrigger

'System.Windows.Interactivity.EventTrigger' must have IsFrozen set to false to modify


I'm trying to execute a command once my storyboard is completed. But doing this I get an InvalidOperationException: 'Specified value of type 'System.Windows.Interactivity.EventTrigger' must have IsFrozen set to false to modify.'

Here is my code:

<ItemsControl x:Name="ItemsControl" 
                  HorizontalAlignment="Right"
                  VerticalAlignment="Bottom"
                  Width="250">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border x:Name="MainBorder" 
                        Background="Gray"
                        Margin="10"
                        Height="100">
                    <Border.RenderTransform>
                        <TranslateTransform X="0"/>
                    </Border.RenderTransform>
                    <Button BorderThickness="0"
                            BorderBrush="Transparent"
                            Background="Transparent"
                            Foreground="Black"
                            Width="20"
                            Height="20"
                            HorizontalAlignment="Right"
                            VerticalAlignment="Top"
                            Content="x">
                        <Button.Triggers>
                                    <EventTrigger RoutedEvent="Button.Click">
                                        <BeginStoryboard>
                                    <Storyboard>
                                                <i:Interaction.Triggers>
                                                    <i:EventTrigger EventName="Completed">
                                                        <i:InvokeCommandAction Command="{Binding DataContext.ClearToastCommand, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                                                                       CommandParameter="{Binding .}"/>
                                                    </i:EventTrigger>
                                                </i:Interaction.Triggers>
                                                <DoubleAnimation By="260" 
                                                                 Duration="0:0:1"
                                                                 Storyboard.TargetName="MainBorder"
                                                                 Storyboard.TargetProperty="RenderTransform.X">
                                                    <DoubleAnimation.EasingFunction>
                                                        <PowerEase EasingMode="EaseOut" />
                                                    </DoubleAnimation.EasingFunction>
                                                </DoubleAnimation>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </Button.Triggers>
                            </Button>
                        </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.Items>
            <system:String>ItemsControl Item #1</system:String>
        </ItemsControl.Items>
    </ItemsControl>

Thanks for any help!


Solution

  • Ok, found a working solution by putting the i: stuff outside of the Storyboard and using the SourceName in EventTrigger.

            <ItemsControl x:Name="ItemsControl" 
                      HorizontalAlignment="Right"
                      VerticalAlignment="Bottom"
                      Width="250">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border x:Name="MainBorder" 
                            Background="Gray"
                            Margin="10"
                            Height="100">
    
                        <Border.RenderTransform>
                            <TranslateTransform X="0"/>
                        </Border.RenderTransform>
                        <Button BorderThickness="0"
                                BorderBrush="Transparent"
                                Background="Transparent"
                                Foreground="Black"
                                Width="20"
                                Height="20"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Top"
                                Content="x">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Completed" 
                                                SourceName="sb">
                                    <i:InvokeCommandAction Command="{Binding DataContext.ClearToastCommand, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                                                           CommandParameter="{Binding .}"/>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                            <Button.Triggers>
                                <EventTrigger RoutedEvent="Button.Click">
                                    <BeginStoryboard>
                                        <Storyboard x:Name="sb">
                                            <DoubleAnimation By="260" 
                                                             Duration="0:0:1"
                                                             Storyboard.TargetName="MainBorder"
                                                             Storyboard.TargetProperty="RenderTransform.X">
                                                <DoubleAnimation.EasingFunction>
                                                    <PowerEase EasingMode="EaseOut" />
                                                </DoubleAnimation.EasingFunction>
                                            </DoubleAnimation>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Button.Triggers>
                        </Button>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.Items>
                <system:String>ItemsControl Item #1</system:String>
            </ItemsControl.Items>
        </ItemsControl>