0

so I have a contentcontrol that has a routedevent:

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));            
    }
}

this event is fired when a dependency property is changed. What I want is to be able to fire an animation when the event fires.

In my resource file for the control I have the following exert that (I thought) would call see the event and start the animation:

<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>

The animation will work if I use the ContentControl.Loaded (only the first time the control's property is changed though), but if I try and register it for my event nothing happens.

Is this possible, am I going about it completely wrong? I hope this made sense.

Thanks

Jon
  • 15,110
  • 28
  • 92
  • 132

1 Answers1

1

Why not trigger your animation when the DependnecyProperty changes instead of using an Event?

I have done this in the past by attaching an event to the DependencyPropertyDescriptor in the Constructor

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();
    }
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • That would be way better... didn't realize you could find the control if you were on the contentcontrol, was thinking it was going down the tree, but thinking about it now maybe it is at the same level of tree? Will try that out. Thanks – Jon Mar 25 '11 at 20:58
  • unfortunately I could not get this to work, also was null: object fadeIn = FindName("fadeInBackGround"); object mygrid = FindName("backdDropGlow"); object fadeRes = TryFindResource("fadeInBackGround"); object mygridRes = TryFindResource("backdDropGlow"); – Jon Mar 28 '11 at 08:53
  • @Jon how/where is your storyboard defined in your XAML? – Rachel Mar 28 '11 at 12:08