gpt4 book ai didi

c# - 当 ContentControl.Content 改变时开始动画

转载 作者:太空狗 更新时间:2023-10-30 00:08:15 25 4
gpt4 key购买 nike

我试图在内容控件(例如 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 时会触发哪个事件。有什么想法吗?

最佳答案

你可以只写一个附加属性:

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>

它比新控件更简单、更短。

关于c# - 当 ContentControl.Content 改变时开始动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10831965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com