开发者

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

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜