Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Exception caused by trigger in grid when trying to animate? (The given object must be an instance of TriggerAction or a derived type)

Ask Question

Here is some simplified XAML. On trying to run the program, I get an exception stating:

'Add value to collection of type 'System.Windows.TriggerActionCollection' threw an exception.' Line number '106' and line position '53'. ---> System.ArgumentException: The given object must be an instance of TriggerAction or a derived type.

Why is this happening?

<Grid x:Name="LoginBoxGrid" Width="400" Height="88" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>                          
    <Grid.Triggers>
        <Trigger Property="UIElement.IsVisible" Value="True">
            <Trigger.ExitActions>
                <Storyboard >
                    <DoubleAnimation Storyboard.TargetName="UNameBoxTranslate" 
                        Storyboard.TargetProperty="X" From="0" To="-800" Duration="0:0:0.5"/>
                    <DoubleAnimation Storyboard.TargetName="UNameBoxTranslate" 
                        Storyboard.TargetProperty="Y" From="0" To="-800" Duration="0:0:0.5"/>
                </Storyboard>
            </Trigger.ExitActions>
        </Trigger>
    </Grid.Triggers>
    <TextBox >                      
        <TextBox.RenderTransform>
            <TranslateTransform x:Name="UNameBoxTranslate"/>
        </TextBox.RenderTransform>
    </TextBox>      
</Grid>

Wrap your Storyboard in a BeginStoryboard because Storyboard is not an ExitAction but BeginStoryboard is.

<BeginStoryboard>
    <Storyboard>
        <!-- ... -->
    </Storyboard>
</BeginStoryboard>

Edit:

Because exit actions can only be used in styles and control templates, this example would have to be reorganized a little bit. Here is one way to do that: use a ContentControl as a vanilla template and fill it will the contents above. Unfortunately now the names are now buried inside a template expansion, but that's a different question since I don't know exactly how they are intended to be used.

<ContentControl>
    <ContentControl.Template>
        <ControlTemplate>
            <Grid x:Name="LoginBoxGrid" Width="400" Height="88" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="300"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBox >
                    <TextBox.RenderTransform>
                        <TranslateTransform x:Name="UNameBoxTranslate"/>
                    </TextBox.RenderTransform>
                </TextBox>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="UIElement.IsVisible" Value="True">
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard >
                                <DoubleAnimation Storyboard.TargetName="UNameBoxTranslate" 
                                    Storyboard.TargetProperty="X" From="0" To="-800" Duration="0:0:0.5"/>
                                <DoubleAnimation Storyboard.TargetName="UNameBoxTranslate" 
                                    Storyboard.TargetProperty="Y" From="0" To="-800" Duration="0:0:0.5"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>
                I did so and got a new error message: 'Initialization of 'System.Windows.Controls.Grid' threw an exception.' Line number '121' and line position '7'. ---> System.InvalidOperationException: Triggers collection members must be of type EventTrigger. Surely it can't be as sinister as it sounds? :(
– ForeverLearnNeverMaster
                May 1, 2011 at 20:36
                Sadly, you cannot use enter and exit action triggers directly in an element.  You can only use them in a control template or in a style.  I'll edit my answer to give you an example of how to approach it.
– Rick Sladkey
                May 1, 2011 at 20:53
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.