将XAML EventTrigger转换为C#代码

时间:2009-11-28 12:38:26

标签: c# wpf xaml eventtrigger

有谁能告诉我这在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>

我不需要“RoutedEvent”-Part因为我想在用户以编程方式点击按钮时执行代码(Click-EventHandler)..首先调用RoutedEvent Handler这就是为什么我不能做一个if-else,需要在代码隐藏中完成整个动画。

提前致谢!

MemphiZ

1 个答案:

答案 0 :(得分:3)

解决了!

只需给故事板添加一个x:名称,然后像这样调用它:

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