ControlTemplate EventTrigger在控件初始化时未触发

时间:2012-10-15 21:27:40

标签: c# .net wpf xaml controltemplate

我有一个使用EventTriggers来启动/停止动画的Control。一切正常,除了第一次初始化控件时,不会触发EventTriggers。我怀疑当属性更改值时它们尚未设置,因此不会触发并启动动画。

如果我在运行时更改IsBusy属性值,一切正常。

我做错了什么?


导致问题的用法(触发了StartEvent,但EventTrigger似乎没有捕获它):

 <my:BusyIndicator IsBusy="True" x:Name="testIndicator" Height="100" Width="100"/>

我控制的相关代码:

    /// <summary>
    /// Event that starts the animation
    /// </summary>
    public static readonly RoutedEvent StartEvent;

    /// <summary>
    /// Event that stops the animation
    /// </summary>
    public static readonly RoutedEvent StopEvent;

    static BusyIndicator()
    {
        DefaultStyleKeyProperty.
            OverrideMetadata(
            typeof(BusyIndicator),
            new FrameworkPropertyMetadata(typeof(BusyIndicator))
        );

        StartEvent =
            EventManager.RegisterRoutedEvent(
            "StartEvent",
            RoutingStrategy.Direct,
            typeof(RoutedEventHandler),
            typeof(BusyIndicator));

        StopEvent =
            EventManager.RegisterRoutedEvent(
            "StopEvent",
            RoutingStrategy.Direct,
            typeof(RoutedEventHandler),
            typeof(BusyIndicator));
    }

    /// <summary>
    /// Indicates if the control is currently animated.
    /// 
    /// This is a dependency property
    /// </summary>
    public bool IsBusy
    {
        get { return (bool)GetValue(IsBusyProperty); }
        set { SetValue(IsBusyProperty, value); }
    }

    /// <summary>
    /// Identifier for the IsBusy property
    /// </summary>
    public static readonly DependencyProperty IsBusyProperty =
        DependencyProperty.Register(
            "IsBusy",
            typeof(bool),
            typeof(BusyIndicator),
            new UIPropertyMetadata(false, new PropertyChangedCallback(OnIsBusyChanged)));

    private static void OnIsBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs inEventArgs)
    {
        bool newValue = (bool)inEventArgs.NewValue;
        if (newValue)
        {
            System.Diagnostics.Debug.WriteLine("Start");
            (sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StartEvent));
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Stop");
            (sender as BusyIndicator).RaiseEvent(new RoutedEventArgs(StopEvent));
        }
    }

我的Themes \ generic.xaml文件中的相关XAML

<Storyboard x:Key="rotateAnimation">
    <DoubleAnimationUsingKeyFrames
                            BeginTime="00:00:00"
                            Storyboard.TargetName="pathCircular"
                            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(RotateTransform.Angle)"
                            RepeatBehavior="Forever">
        <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
        <SplineDoubleKeyFrame KeyTime="00:00:01" Value="360"/>
    </DoubleAnimationUsingKeyFrames>
</Storyboard>

<Style TargetType="{x:Type local:BusyIndicator}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:BusyIndicator}">
                <Grid Height="{TemplateBinding Height}"
                      Width="{TemplateBinding Width}"
                      Margin="{TemplateBinding Margin}">
                    <Path Stretch="Fill" Name="pathCircular"
                          <!-- the path being animated -->
                    </Path>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="local:BusyIndicator.StopEvent">
                        <StopStoryboard BeginStoryboardName="rotateAnimation"/>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="local:BusyIndicator.StartEvent">
                        <BeginStoryboard Name="rotateAnimation" Storyboard="{StaticResource rotateAnimation}"/>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

如果尚未应用模板,则无法收听该事件。您需要在 OnApplyTemplate 之后保留一个标记并引发事件,或者使用Trigger到IsBusy属性并使用 EnterActions ExitActions