【问题标题】:Storyboard animation based on control template's control property [duplicate]基于控件模板的控件属性的情节提要动画[重复]
【发布时间】:2009-11-25 16:03:06
【问题描述】:

这就是我所拥有的

  1. 我的 window.resource 中一个按钮的 ControlTemplate,有一个带有外发光的椭圆(名为 -- TasksToggleButtonOuterGlowBitmapEffect)和一个文本
  2. 使用此模板的按钮

这就是我需要的

1.在上述椭圆的外发光上运行的情节提要 2. 我会随时从我的代码隐藏文件中触发这个故事板

当我使用以下 sn-p 尝试此操作时,框架给了我一个运行时异常,指出它无法找到控件,TasksToggleButtonOuterGlowBitmapEffect

<Window.Resource>
    <ControlTemplate x:Key="DefaultTasksToggleButtonTemplate" TargetType="ToggleButton">
        <Grid Margin="2">
            <Border BorderBrush="White" BorderThickness="2" CornerRadius="20">
                <Border.BitmapEffect>
                    <OuterGlowBitmapEffect x:Name="TasksToggleButtonOuterGlowBitmapEffect" GlowColor="LightGray" />
                </Border.BitmapEffect>
            </Border>
            <Ellipse Fill="Red" Width="20" Height="20" Margin="2" />
            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </ControlTemplate>


    <Storyboard x:Key="GlowStoryboard">
        <DoubleAnimation Storyboard.TargetName="TasksToggleButtonOuterGlowBitmapEffect" Storyboard.TargetProperty="GlowSize" From="5" To="10" />
    </Storyboard>

</Window.Resources>

更新 -- 我希望它出现在资源中,以便任何按钮都可以使用它

【问题讨论】:

    标签: c# wpf animation storyboard


    【解决方案1】:

    从表面上看,这对我来说是使用 Visual State Manager 的理想选择。如果触发器将是 MouseOver、Pressed 等,则动画对于这种效果有点笨拙。如果您打算对 Button State 做出反应,那么一定要看 VSM。

    您遇到的问题是您尝试访问的目标是模板的一部分,因此不是真正的元素。你不能这样做是有道理的:想象你有 50 个按钮都应用相同的模板:动画如何知道你真正指的是哪个 TasksToggleButtonOuterGlowBitmapEffect?

    您也许可以在触发器中处理此问题(如果这是 WPF),但我认为这样做还需要在模板中定义动画,我不知道您是否可以这样做.

    您还可以在运行时在代码后面导航模板树并在那里设置您的动画。

    【讨论】:

    • 刚刚看到 PanJanek 发布的链接:该帖子有答案。我不知道您可以将模板传递给动画,这很酷!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    相关资源
    最近更新 更多