找不到EventTrigger.SourceName

时间:2012-04-09 00:14:14

标签: wpf animation storyboard eventtrigger

<Canvas>
    <Button Name="b1" Content="Button"/>
    <Rectangle Name="Mr1" Fill="Black">
        <Rectangle.Triggers>
            <EventTrigger SourceName="b1" RoutedEvent="Button.Click">
                <BeginStoryboard>
                 <code.../>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
    </Rectangle>
</Canvas>

错误:找不到b1。

请帮帮我,谢谢。

2 个答案:

答案 0 :(得分:3)

我刚刚面临同样的问题..为其他感兴趣的人发布解决方案。

<Canvas>

    <Canvas.Resources>
        <Storyboard x:Key="MyAwesomeAnimation" Storyboard.TargetName="Mr1" Storyboard.TargetProperty="Height">
            <!-- Some Awesome animation here..-->
        </Storyboard>
    </Canvas.Resources>

    <Button Name="b1" Content="Button">
        <Button.Triggers>
            <!--The key is to define the click trigger on the button. Why should the rectangle know about the button?-->
            <EventTrigger RoutedEvent="Button.Click">
                <BeginStoryboard Storyboard="{StaticResource MyAwesomeAnimation}"></BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>

    <Rectangle Name="Mr1" Fill="Black" />

</Canvas>

答案 1 :(得分:1)

  

您可以将此属性设置为应用触发器集合(此触发器所属的集合)范围内的任何元素的名称。这通常是包含此触发器的模板中的命名元素。