【问题标题】:Why is this ColorAnimation not starting?为什么这个 ColorAnimation 没有启动?
【发布时间】:2012-08-14 03:36:27
【问题描述】:

我正在尝试根据控件的状态发生 ColorAnimation。我为 ToggleButton 创建了一个 ControlTemplate,看起来像这样:

<ControlTemplate TargetType="ToggleButton">
    <Border>
    <Grid>
        <VisualStateManager.VisualStateGroup>
             <VisualStateGroup x:Name="CommonStates">
                  <VisualState x:Name="MouseOver">
                      <Storyboard>
                           <ColorAnimation Storyboard.TargetName="ColorChangeRect" 
                               Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)"
                               To="Blue" Duration="0:0:3" />
                      </Storyboard>
                  </VisualState>
             </VisualStateGroup>
        </VisualStateManager.VisualStateGroup>
        <Rectangle x:Name=ColorChangeRect" Fill="Red" />
        <ContentPresenter Content="{TemplateBinding Content}" />
    </Grid>
    </Border>
</ControlTemplate>

不幸的是,什么都没有发生。我不确定我在这里缺少什么。

【问题讨论】:

  • 哇哇 D:我现在清楚地看到了我的问题。我应该在我的办公桌上放一只橡皮鸭。 @_@

标签: wpf controltemplate togglebutton visualstatemanager coloranimation


【解决方案1】:

将VisualStateManager.VisualStateGroups 附加属性放在模板的根元素上,即代码中的Border。这就是VisualStateManager 获取状态的地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2021-09-12
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多