更改ContentControl.Content时开始动画

时间:2012-05-31 10:39:17

标签: c# wpf xaml

我正在尝试在Button或ContentControl等内容控件更改其内容时触发动画。我最初的想法是这样做:

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

            <Button Content="Hello"/>
        </ContentControl>

但我不知道更改/更新ContentPresenter时会触发哪个事件。有什么想法吗?

2 个答案:

答案 0 :(得分:11)

你可以写一个附属物:

static class ContentControlExtensions
{
    public static readonly DependencyProperty ContentChangedAnimationProperty = DependencyProperty.RegisterAttached(
        "ContentChangedAnimation", typeof(Storyboard), typeof(ContentControlExtensions), new PropertyMetadata(default(Storyboard), ContentChangedAnimationPropertyChangedCallback));

    public static void SetContentChangedAnimation(DependencyObject element, Storyboard value)
    {
        element.SetValue(ContentChangedAnimationProperty, value);
    }

    public static Storyboard GetContentChangedAnimation(DependencyObject element)
    {
        return (Storyboard)element.GetValue(ContentChangedAnimationProperty);
    }

    private static void ContentChangedAnimationPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        var contentControl = dependencyObject as ContentControl;
        if (contentControl == null)
            throw new Exception("Can only be applied to a ContentControl");

        var propertyDescriptor = DependencyPropertyDescriptor.FromProperty(ContentControl.ContentProperty,
            typeof (ContentControl));

        propertyDescriptor.RemoveValueChanged(contentControl, ContentChangedHandler);
        propertyDescriptor.AddValueChanged(contentControl, ContentChangedHandler);
    }

    private static void ContentChangedHandler(object sender, EventArgs eventArgs)
    {
        var animateObject = (FrameworkElement) sender;
        var storyboard = GetContentChangedAnimation(animateObject);
        storyboard.Begin(animateObject);
    }
}

然后在XAML中:

        <ContentControl Content="{Binding SelectedViewItem}">
            <extensions:ContentControlExtensions.ContentChangedAnimation>
                <Storyboard>
                    <ThicknessAnimation To="0" From="30,0,-30,0" Duration="0:0:0.3" Storyboard.TargetProperty="Margin"/>
                </Storyboard>
            </extensions:ContentControlExtensions.ContentChangedAnimation>
        </ContentControl>

它比新控件更容易和更短。

答案 1 :(得分:1)

不幸的是,ContentChanged没有CLR事件(更不用说EventTriggers需要的RoutedEvent)。但是,鉴于您正在处理自定义控件,您可以覆盖Content属性的元数据,并在控件中提供您自己的回调。

这可能与您正在寻找的内容有关here

显然,他创建了一个CLR事件来向外传播内容更改;你也可以使用RoutedEvent来做同样的事情。

对OverrideMetadata here

的补充阅读