日期:2014-05-18  浏览次数:21257 次

能帮忙看下EventTrigger为什么没有触发么?
我定义了以下简单CustomerControl:
XML code

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StoryboardTest">
    
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Viewbox>
                        <Grid Background="LightBlue" x:Name="theGrid">
                            <Rectangle x:Name="faceRect" Width="50" Height="50" >
                                <Rectangle.RenderTransform>
                                    <ScaleTransform CenterX="25" CenterY="25" ScaleX="1" ScaleY="1"/>
                                </Rectangle.RenderTransform>
                            </Rectangle>
                        </Grid>
                    </Viewbox>
                    
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Rectangle.MouseEnter" SourceName="faceRect">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="faceRect"
                                                     Storyboard.TargetProperty="Width"
                                                     To="100"/>
                                    <DoubleAnimation Storyboard.TargetName="faceRect"
                                                     Storyboard.TargetProperty="Height"
                                                     To="100"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Rectangle.MouseLeave" SourceName="faceRect">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="faceRect"
                                                     Storyboard.TargetProperty="Width"
                                                     To="50"/>
                                    <DoubleAnimation Storyboard.TargetName="faceRect"
                                                     Storyboard.TargetProperty="Height"
                                                     To="50"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

想实现鼠标进入或出控件中的Rectangle时候,分别播放动画,但是,运行却没有反应,如果将EventTrigger的RoutedEvent更改为:<EventTrigger RoutedEvent="Grid.MouseEnter">以及<EventTrigger RoutedEvent="Grid.MouseLeave">则可以触发,请问这是为什么?多谢

------解决方案--------------------
有个Fill属性吧,反正就是背景色,如果全透明的话,就捕获不到鼠标的。

你试试,我也没测试过