尝试制作动画时网格中的触发器导致异常? (给定的对象必须是 TriggerAction 的实例或派生类型)

发布于 2024-11-04 03:08:10 字数 1507 浏览 1 评论 0原文

这是一些简化的 XAML。在尝试运行该程序时,我收到一个异常,指出:

“向‘System.Windows.TriggerActionCollection’类型的集合添加值引发了异常。”行号“106”和行位置“53”。 ---> System.ArgumentException:给定的对象必须是 TriggerAction 的实例或派生类型。

为什么会发生这种情况?

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃梦幻 2024-11-11 03:08:10

Storyboard 包装在 BeginStoryboard 中,因为 Storyboard 不是 ExitAction,但 BeginStoryboard 是。

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

编辑:

由于退出操作只能在样式和控件模板中使用,因此必须稍微重新组织此示例。这是一种方法:使用 ContentControl 作为普通模板并填充上面的内容。不幸的是,现在这些名称被隐藏在模板扩展中,但这是一个不同的问题,因为我不确切知道它们的用途。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文