将 XAML EventTrigger 转换为 C# 代码

发布于 2024-08-12 23:52:11 字数 994 浏览 4 评论 0原文

谁能告诉我这在 C# 中会是什么样子?

<EventTrigger RoutedEvent="Button.Click" SourceName="btnSplit">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="gridSplit" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
                            <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="gridJoin" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>

我不需要“RoatedEvent”部分,因为我想在用户以编程方式单击按钮时执行代码(Click-EventHandler)。首先调用 RoutedEvent 处理程序,这就是为什么我不能执行 if-否则,需要在代码隐藏中完成整个动画。

提前致谢!

孟菲斯

Can anyone tell me how this would look like in C#?

<EventTrigger RoutedEvent="Button.Click" SourceName="btnSplit">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="gridSplit" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
                            <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="gridJoin" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>

I don't need the "RoutedEvent"-Part because I want to execute the Code when the User Clicks on a Button programmatically (Click-EventHandler).. The RoutedEvent Handler gets called first which is why I can't do a if-else and need to do the whole Animation in code-behind.

Thanks in advance!

MemphiZ

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

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

发布评论

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

评论(1

百变从容 2024-08-19 23:52:11

解决了!

只需给情节提要一个 x:Name,然后像这样调用它:

<Storyboard x:Name="storyBoardName">
    <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="grid1" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
    <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="grid2" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
</Storyboard>

Storyboard story = (Storyboard)this.FindResource("storyBoardName");  
this.BeginStoryboard(story);

Solved it!

Just give the Storyboard an x:Name and then call it like this:

<Storyboard x:Name="storyBoardName">
    <DoubleAnimation From="0" To="1" Duration="0:0:0.25" Storyboard.TargetName="grid1" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
    <DoubleAnimation From="1" To="0" Duration="0:0:0.25" Storyboard.TargetName="grid2" Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
</Storyboard>

Storyboard story = (Storyboard)this.FindResource("storyBoardName");  
this.BeginStoryboard(story);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文