none
在ControlTemplate的Triggers中加入EventTrigger,然后在其中的Storyboard中给Completed事件绑定回调方法。但是发现动画结束后并不能触发Completed事件? RRS feed

  • 问题

  • 如题,动画能够执行,但是并不能触发Completed事件。在代码中打断点发现根本不往里面走。这是为什么呢?
    <ControlTemplate TargetType="ToggleButton" x:Key="ToggleButtonTemplate">
                <Grid Background="Transparent">
                    <Rectangle x:Name="rec1" Width="30" Height="30" Fill="Yellow"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="ToggleButton.Checked">
                        <BeginStoryboard>
                            <Storyboard Completed="Storyboard_Completed">
                                <DoubleAnimation Storyboard.TargetName="rec1" Storyboard.TargetProperty="Width" To="15" Duration="0:0:2"/>
                                <DoubleAnimation Storyboard.TargetName="rec1" Storyboard.TargetProperty="Height" To="15" Duration="0:0:2"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

    2019年3月20日 13:18

全部回复

  • 将Storyboard提取出单独放在资源或者资源字典中即可

      <Storyboard x:Key="SB"  Completed="Storyboard_Completed">
                <DoubleAnimation Storyboard.TargetName="rec1" Storyboard.TargetProperty="Width" To="15" Duration="0:0:2"/>
                <DoubleAnimation Storyboard.TargetName="rec1" Storyboard.TargetProperty="Height" To="15" Duration="0:0:2"/>
            </Storyboard>
    
    
    <!--ControlTempate的触发器-->
    
         <EventTrigger RoutedEvent="ToggleButton.Checked" >
                                    
    <BeginStoryboard Storyboard="{StaticResource SB}"/>  
                            
      </EventTrigger>

    2019年3月21日 1:55
  • Hello,

    根据你的描述, 我们可以看到你把StoryBoard放入样式中,放入样式的StoryBoard将被冻结, 当StoryBoard被冻结时,它处于不可修改的状态,这就是为什么你不能将事件处理程序添加到Complete事件。

    所以你可以尝试直接为控件添加Trigger触发器, 不需要经过样式或者ControlTemplate, 这样就可以触发Complete事件。

    还有一种方法就是把StoryBoard放在Resource里面, 我做了一个例子你可以看一下:

     <Window.Resources>
            <Storyboard x:Key="storyBoard" Completed="Storyboard_Completed">
                <DoubleAnimation
                    Storyboard.TargetName="faceEllipse"
                    Storyboard.TargetProperty="Width"
                    To="600"
                    Duration="0:0:0:1" />
            </Storyboard>
            <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
                <!--  定义视觉树  -->
                <Grid>
                    <Ellipse
                        Name="faceEllipse"
                        Width="{TemplateBinding Control.Width}"
                        Height="{TemplateBinding Control.Height}"
                        Fill="{TemplateBinding Button.Background}" />
                    <TextBlock
                        Name="txtBlock"
                        Margin="{TemplateBinding Button.Padding}"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Text="{TemplateBinding Button.Content}" />
                </Grid>
    
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard Storyboard="{StaticResource storyBoard}" />
                    </EventTrigger>
    
                </ControlTemplate.Triggers>
                <!--  定义触发器_End  -->
            </ControlTemplate>
        </Window.Resources>
        <Grid>
            <Button
                x:Name="Button1"
                Width="100"
                Height="60"
                Content="Click Me"
                Template="{StaticResource ButtonTemplate}">
                <!--<Button.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard>
                            <Storyboard Completed="Storyboard_Completed">
                                <DoubleAnimation
                                    Storyboard.TargetProperty="Width"
                                    To="200"
                                    Duration="0:0:0:1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="Width"
                                    To="100"
                                    Duration="0:0:0:1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Button.Triggers>-->
    
            </Button>
        </Grid>


    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2019年3月21日 3:18
    版主
  • 谢谢回复,给了我很大帮助。
    2019年3月21日 11:02
  • 谢谢版主回复,之前不清楚Storyboard直接写在里面会冻结,刚才试了一下,把Storyborad单独提取出来作为资源确实可行。十分感谢。
    2019年3月21日 11:13
  • Hello,

    如果你的问题已经解决, 请点击对你有帮助的帖子下面的“标记为答复”来关闭你的帖子, 这将有益于以后遇到相同问题的人, 谢谢你的理解。

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年3月22日 1:09
    版主