【发布时间】:2013-11-13 03:37:42
【问题描述】:
我有一个 ToggleButton,我单击它会弹出一个 FlowDocumentReader 作为 Adorner。此 FlowDocument 是带有 DataTrigger 的 ControlTemplate 的一部分,用于显示/隐藏元素。
使用以下触发器一切正常。我使用了 DataTrigger 和一些 Setter,当我检查我的 ToggleButton 时,我的元素使用我提供的高度和宽度正确显示:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
<Setter TargetName="mainBorder" Property="Height" Value="437"></Setter>
<Setter TargetName="mainBorder" Property="Width" Value="537"></Setter>
</DataTrigger>
</ControlTemplate.Triggers>
我希望在我的元素出现时出现一些动画,所以我尝试使用情节提要。这不起作用,似乎什么也没发生:
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="mainBorder">
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Width" To="537" />
<DoubleAnimationUsingKeyFrames BeginTime="0:0:0.2" Duration="0:0:0.3" Storyboard.TargetProperty="Height">
<LinearDoubleKeyFrame Value="417" KeyTime="0:0:0.2" />
<LinearDoubleKeyFrame Value="437" KeyTime="0:0:0.24" />
<LinearDoubleKeyFrame Value="417" KeyTime="0:0:0.3" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="mainBorder">
<DoubleAnimationUsingKeyFrames Duration="0:0:0.2" Storyboard.TargetProperty="Width">
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.2" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="0:0:0.2" Duration="0:0:0.2" Storyboard.TargetProperty="Height">
<LinearDoubleKeyFrame Value="0" KeyTime="0:0:0.2" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</ControlTemplate.Triggers>
Storyboard 上下文是否与 Setter 完全不同?为什么它在一个地方有效,而在另一个地方无效?
奇怪的是,当我进行此更改时,会导致绑定错误显示在输出窗口中。我没有触及 DataTrigger 的实际绑定,只是内容:
System.Windows.Data 错误:4:找不到绑定源 参考“元素名称=装饰者”。 BindingExpression:Path=AdornedElement.IsChecked;数据项=空;目标 元素是'控制'(名称='');目标属性是“NoTarget”(类型 '对象')
以下是模板其余部分的总体思路:
<ControlTemplate x:Key="LocalHelpWindow">
<Grid>
<Grid.RowDefinitions>
...
</Grid.RowDefinitions>
<help:AdornedPlaceholder x:Name="adorner" Grid.Row="0"/>
<Border Grid.Row="1" x:Name="mainBorder">
...
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner, Path=AdornedElement.IsChecked}" Value="True" >
...
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
【问题讨论】:
标签: wpf storyboard controltemplate datatrigger