我正在尝试让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>不幸的是,什么都没有发生。我不确定我错过了什么。
发布于 2012-08-14 03:36:28
将VisualStateManager.VisualStateGroups附加属性放在模板的根元素上,即代码中的Border。这就是VisualStateManager获取状态的地方。
https://stackoverflow.com/questions/11940974
复制相似问题