为什么这个ColorAnimation没有启动?

时间:2012-08-13 19:27:58

标签: wpf controltemplate togglebutton visualstatemanager coloranimation

我正在尝试根据控件的状态进行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>

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

1 个答案:

答案 0 :(得分:1)

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