我正在尝试为Expander控件创建自己的模板.当控件展开时,我希望内容缓慢滑下.
在编译时不知道内容的期望高度.
我以为我们可以将幻灯片定义为动画:
<Storyboard x:Key="ExpandContent">
<DoubleAnimation
Storyboard.TargetName="_expanderContent"
Storyboard.TargetProperty="Height"
From="0.0"
To="{Binding ElementName=_expanderContent,Path=DesiredHeight}"
Duration="0:0:1.0" />
</Storyboard>
Run Code Online (Sandbox Code Playgroud)
但不幸的是没有.我们收到错误
无法冻结此Storyboard时间轴树以跨线程使用.
看来我们在定义动画参数时不能使用绑定.(也在这个问题中讨论过.)
有没有人对我如何处理这个有任何想法?我担心使用LayoutTransform.ScaleY,因为这会产生扭曲的图像.
这与此问题类似,但这个问题的答案涉及编写代码隐藏,我认为在控件模板中是不可能的.我想知道基于XAML的解决方案是否可以实现.
<ControlTemplate x:Key="ExpanderControlTemplate" TargetType="{x:Type Expander}">
<ControlTemplate.Resources>
<!-- Here are the storyboards which don't work -->
<Storyboard x:Key="ExpandContent">
<DoubleAnimation
Storyboard.TargetName="_expanderContent"
Storyboard.TargetProperty="Height"
From="0.0"
To="{Binding ElementName=_expanderContent,Path=DesiredHeight}"
Duration="0:0:1.0" />
</Storyboard>
<Storyboard x:Key="ContractContent">
<DoubleAnimation
Storyboard.TargetName="_expanderContent"
Storyboard.TargetProperty="Height"
From="{Binding ElementName=_expanderContent,Path=DesiredHeight}"
To="0.0"
Duration="0:0:1.0" />
</Storyboard>
</ControlTemplate.Resources>
<Grid Name="MainGrid" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Name="ContentRow" Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" …Run Code Online (Sandbox Code Playgroud) 我的组合框在我的应用程序中就像扩展器一样.当我需要折叠一个groupbox时,我将它的高度设置为0.当我需要展开它时,我将它的高度设置为auto(double.Nan)是否可以用故事板来实现.我怎么能提前知道自动高度.表达式混合不能使我动画到自动.
