这是我的自定义按钮:
<Style TargetType="local:AnswerButton">
<Setter Property="Background" Value="{StaticResource BlueGradient}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:AnswerButton">
<Grid>
<Border BorderBrush="Blue" BorderThickness="2" CornerRadius="10">
<Border Name="myBorder" Background="{TemplateBinding Background}" CornerRadius="9">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
TextAlignment="Center" VerticalAlignment="Center"
Text="{TemplateBinding Option}" Foreground="Yellow" />
<TextBlock Grid.Column="1"
TextAlignment="Left" VerticalAlignment="Center"
Text="{TemplateBinding Text}" Foreground="Black" />
</Grid>
</Border>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如何为自定义按钮设置动画?我试过这个:
ColorAnimation myColorAnimation = new ColorAnimation();
myColorAnimation.From = Colors.Blue;
myColorAnimation.To = Colors.Green;
myColorAnimation.AutoReverse = true;
myColorAnimation.Duration = TimeSpan.FromSeconds(1);
Storyboard.SetTargetName(myColorAnimation, "myBorder");
Storyboard.SetTargetProperty(myColorAnimation,
new PropertyPath(Border.BackgroundProperty));
Storyboard myStoryboard = new Storyboard();
myStoryboard.Children.Add(myColorAnimation);
myStoryboard.Begin();
但它的目标名称有问题。我知道这是错误的,但是如何将目标设置为自定义控件?我的页面中有 4 个,我想为选择的一个设置此动画:
<Controls:AnswerButton Name="btnAnswerA" Tap="AnswerButton_Tap"/>
<Controls:AnswerButton Name="btnAnswerB" Tap="AnswerButton_Tap"/>
<Controls:AnswerButton Name="btnAnswerC" Tap="AnswerButton_Tap"/>
<Controls:AnswerButton Name="btnAnswerD" Tap="AnswerButton_Tap"/>
我不在乎它是在代码中还是在 xaml 中,但我怎样才能让一些自定义按钮通过 coloranimation 闪烁(闪烁)?谢谢
编辑:我在 Thaven 的回答中尝试了许多使用 visualstatemanager 的选项,但没有帮助。真的没有人知道哪里可能出现问题吗?