0

所以我有一个具有路由事件的内容控件:

public class TestBlind : ContentControl
{
public static readonly RoutedEvent VisibilityVisibleEvent =
        EventManager.RegisterRoutedEvent("VisibilityVisible", RoutingStrategy.Tunnel, typeof(Visibility), typeof(TestBlind));

    public event RoutedEventHandler VisibilityVisible
    {
        add { AddHandler(VisibilityVisibleEvent, value); }
        remove { RemoveHandler(VisibilityVisibleEvent, value); }
    }

    [Category("TestBlind")]
    public bool IsContentVisible
    {
        get { return (bool)GetValue(IsContentVisibleProperty); }
        set { SetValue(IsContentVisibleProperty, value); }
    }

    public static readonly DependencyProperty IsContentVisibleProperty = DependencyProperty.Register("IsContentVisible", typeof(bool), typeof(TestBlind),
        new FrameworkPropertyMetadata(new PropertyChangedCallback(OnIsOverlayContentVisibleChanged)));


    private static void OnIsOverlayContentVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TestBlind blind = d as TestBlind;
        if (blind != null)
            SetVisibility(blind);
    }

    private static void SetVisibility(TestBlind blind)
    { 
        blind.Visibility = blind.IsContentVisible ? Visibility.Visible : Visibility.Hidden;
        blind.RaiseEvent(blind.Visibility == Visibility.Visible ? new RoutedEventArgs(VisibilityVisibleEvent) : new RoutedEventArgs(VisibilityHiddenEvent));            
    }
}

更改依赖项属性时会触发此事件。我想要的是能够在事件触发时触发动画。

在我的控件资源文件中,我有以下操作(我认为)会调用查看事件并启动动画:

<Grid.Triggers>
                        <EventTrigger RoutedEvent="control:TestBlind.VisibilityVisible">
<!--                            <EventTrigger RoutedEvent="ContentControl.Loaded">-->
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Duration="00:00:02.25" BeginTime="00:00:00" Storyboard.TargetName="backdDropGlow" Storyboard.TargetProperty="Opacity">
                                            <SplineDoubleKeyFrame KeyTime="00:00:00.00" Value="0"/>
                                            <SplineDoubleKeyFrame KeyTime="00:00:02.25" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>

如果我使用 ContentControl.Loaded 动画将起作用(尽管仅在第一次更改控件的属性时),但如果我尝试为我的事件注册它,则不会发生任何事情。

这可能吗,我完全错了吗?我希望这是有道理的。

谢谢

4

1 回答 1

1

为什么不在 DependnecyProperty 更改而不是使用事件时触发动画?

我过去通过将事件附加到构造函数中的 DependencyPropertyDescriptor 来完成此操作

public TestBlind()
{
    DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TestBlind.VisibilityVisible, typeof(TestBlind));
    if (dpd != null) dpd.AddValueChanged(this, delegate { IsVisibilityVisibleChanged(); });
}

private void IsVisibilityVisibleChanged()
{
    bool isShown = GetVisibilityVisible(this);
    if (isShown)
    {
        Storyboard animation = (Storyboard)this.FindResource("MyStoryboard");
        animation.Begin();
    }
}
于 2011-03-25T17:09:17.807 回答