补充阅读你可以只写一个附加属性:
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>
这是不是一个新的控制更容易和更短。
如果将'Get..'和'Set ...'方法中的'DependencyObject'标注更改为'ContentControl',则不需要在回调方法中进行类型检查。 – gregsdennis
优秀的例子,很好,很简单....对于新的WPF开发人员来说,它可能只是值得将xmlns:behavior =“clr-namespace:<您的应用程序名称空间>”添加到XAML的顶部..... – Monty
这不是一种行为,而是一种附属财产。 –