下面是一些代码:
<components:AnimatedContentControl x:Name="MainContent" Content="{Binding}">
<components:AnimatedContentControl.Triggers>
<EventTrigger RoutedEvent="components:AnimatedContentControl.ContentChanged">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="MainContent"
Storyboard.TargetProperty="Height"
Duration="0:0:0.25"
From="0"
To="{Binding ElementName=MainContent, Path=ActualHeight}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</components:AnimatedContentControl.Triggers>
</components:AnimatedContentControl>AnimatedContentControl是我创建的一个类,它基于一个ContentControl,其中包含一个ContentChanged路由事件(因为无论出于什么原因,ContentControl在默认情况下都没有ContentChanged事件)。
此代码的目标是将我的应用程序的DataContext指向的任何内容加载到此ContentControl中,并且当数据上下文发生更改时,显示一个简单的动画,该动画由从零到新内容大小的幻灯片组成(理想情况下,它将从旧内容的高度滑行到新内容的高度,但第一步是第一步)。对于这样一个简单的想法,这是非常难以发展的。
我已经知道我的问题是什么:当这个事件触发时,AnimatedContentControl的AnimatedContentControl属性已经被更新了,但是由于某种原因,AnimatedContentControl的ActualSize没有(实际上,想想看,我可能可以将ActualSize属性用于DoubleAnimation的From属性--我稍后会尝试)。
因此,我的问题是:在DoubleAnimation中有什么东西我可以绑定到,即AnimatedContentControl内容的实际大小吗?如果是,它是什么?如果不是,有什么解决办法?
发布于 2013-08-28 00:14:15
鲍里斯是对的-我的代码现在看起来是这样的:
<components:AnimateableContentControl x:Name="MainContent" Content="{Binding}">
<components:AnimateableContentControl.Triggers>
<EventTrigger RoutedEvent="components:AnimateableContentControl.ContentChanged">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
x:Name="TransitionAnimation"
Storyboard.TargetName="MainContent"
Storyboard.TargetProperty="Height"
Duration="0:0:0.25"
From="0"
To="0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</components:AnimateableContentControl.Triggers>
</components:AnimateableContentControl>在...and中,我更改了DataContexts (导致动画),我有以下内容:
DataTemplate template = Resources[new DataTemplateKey(m_currentViewModel.GetType())] as DataTemplate;
if (template == null)
{
DataContext = m_currentViewModel;
return;
}
FrameworkElement element = template.LoadContent() as FrameworkElement;
if (element == null)
{
DataContext = m_currentViewModel;
return;
}
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
TransitionAnimation.To = element.DesiredSize.Height;
DataContext = m_currentViewModel;就像一种魅力!谢谢鲍里斯!
https://stackoverflow.com/questions/18473271
复制相似问题