4

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が変更/更新されたときにどのイベントが発生するかはわかりません。何か案は?

4

2 に答える 2

16

添付プロパティを記述するだけです。

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

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>

新しいコントロールよりもはるかに簡単で短いです。

于 2015-05-30T08:23:56.630 に答える
2

残念ながら、ContentChangedのCLRイベントはありません(EventTriggersに必要なRoutedEventははるかに少ないです)。ただし、カスタムコントロールを処理している場合は、Contentプロパティのメタデータをオーバーライドして、コントロール内に独自のコールバックを提供できます。

これはあなたがここで探しているものについてかもしれません

明らかに、彼はコンテンツの変更を外部に伝播するためのCLRイベントを作成しました。代わりにRoutedEventを使用して同じことを行うこともできます。

OverrideMetadataに関する追加情報はこちら

于 2012-05-31T12:11:56.747 に答える