![](/img/trans.png)
[英]How to use WPF ContentControl 's content with DataTemplate
[英]WPF - How to get ActualHeight of ContentControl's content?
这是一些代码:
<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
的Content
属性已更新,但是由于某种原因, AnimatedContentControl
的ActualSize
尚未(实际上,想到它,我可能会使用ActualSize
属性为DoubleAnimation
的From
属性的属性-我稍后再尝试)。
所以我的问题是: DoubleAnimation
可以绑定哪些东西,它是AnimatedContentControl
内容的实际大小? 如果是这样,那是什么? 如果没有,有哪些解决方法?
鲍里斯是对的-我的代码现在看起来像这样:
<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>
...以及我更改DataContext
的位置(导致动画)的地方,我有:
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;
奇迹般有效! 谢谢鲍里斯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.