mortware mortware - 1 month ago 11
C# Question

Begin animation when ContentControl.Content is changed

I'm trying to fire an animation when a content control such as Button or ContentControl changes its content. My initial thoughts were to do this:

<ContentControl x:Name="ContentElement">
<Style TargetType="ContentControl">
<Setter Property="Template">
<ControlTemplate TargetType="ContentControl">
<ContentPresenter x:Name="Content">
<EventTrigger RoutedEvent="WHATGOESHERE">
<BeginStoryboard Storyboard="{StaticResource MyAnimation}" Storyboard.TargetName="Content"/>

<Button Content="Hello"/>

But I don't know which event fires when the ContentPresenter is changed/updated. Any ideas?


There is no CLR-event for ContentChanged (much less a RoutedEvent required for EventTriggers) unfortunately. However, given that you're dealing with a custom control, you can override the metadata for the Content property and provide your own callback within the control.

This may be about what you're looking for here

Obviously he's created a CLR-event to propagate content changes externally; you could also do the same just using a RoutedEvent instead.

Additional reading on OverrideMetadata here