如何处理动画ControlTemplate样式的子项?

时间:2014-11-06 12:36:04

标签: wpf storyboard wpf-controls datatrigger

我有一个扩展切换按钮的自定义控件。此自定义控件具有ImageSource的三个依赖项属性,可用于自定义呈现特定视觉效果的Image

<Style TargetType="{x:Type local:SimpleFeedbackToggleButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:SimpleFeedbackToggleButton}">
                <Grid x:Name="BtnGrid">
                    <Image x:Name="FeedbackImage" Source="{TemplateBinding FeedbackImageSource}" Visibility="Hidden" />
                    <Image x:Name="NormalImage" Margin="{TemplateBinding Padding}" Source="{TemplateBinding NormalImageSource}" />
                    <Image x:Name="DisabledImage" Source="{TemplateBinding DisabledImageSource}" Visibility="Hidden" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Visibility" Value="Visible" TargetName="DisabledImage"/>
                        <Setter Property="Opacity" Value="1.0" TargetName="BtnGrid"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="true">
                        <Setter Property="Visibility" Value="Visible" TargetName="FeedbackImage"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <DataTrigger Binding="{Binding FeedbackBlink, RelativeSource={RelativeSource Self}}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard x:Name="SB_BlinkFeedback">
                    <Storyboard Storyboard.TargetProperty="Opacity">
                        <DoubleAnimation From="1.0" To="0.3" Duration="0:0:1" RepeatBehavior="Forever" AutoReverse="True" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <StopStoryboard BeginStoryboardName="SB_BlinkFeedback" />
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>    

另一个依赖属性FeedbackBlink将用于启动和停止仅闪烁其中一个图像(FeedbackImage)。很遗憾,我无法找到如何从Storyboard&#34; SB_BlinkFeedback&#34;处理此图片。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

在发布问题后不久找到了一个有效的解决方案:我必须将DataTrigger<Style.Triggers>移到<ControlTemplate.Triggers>。然后Storyboard声明可以由Storyboard.TargetName="FeedbackImage"完成,无需编译器或运行时错误。