Tar*_*ras 48 c# wpf animation colors coloranimation
我需要帮助才能做出正确的决定.当某些事件发生时,我需要为我的用户控件的背景颜色设置动画.如果是,我想改变背景只需1秒然后再将其转回.我该走哪条路?使用颜色动画或计时器或可以通过其他方式.
解决了.谢谢大家!这对我有好处:
ColorAnimation animation;
animation = new ColorAnimation();
animation.From = Colors.Orange;
animation.To = Colors.Gray;
animation.Duration = new Duration(TimeSpan.FromSeconds(1));
this.elGrid.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);
Run Code Online (Sandbox Code Playgroud)
Kla*_*s78 75
我会用EventTrigger一个ColorAnimation.
在此示例中Button Brackground,MouseLeave事件变为绿色.希望此代码与您可能需要的代码类似.
<Button Content="Button" Height="75" HorizontalAlignment="Left" Margin="27,12,0,0" Name="btnImgBrush" VerticalAlignment="Top" Width="160" Background="LightGray">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="Green"
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
FillBehavior="Stop"
Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
Run Code Online (Sandbox Code Playgroud)
Gui*_*ish 26
注意,如果您的背景是冻结的实例,则可能会收到System.InvalidOperationException.
无法为'System.Windows.Media.SolidColorBrush'上的'Color'属性设置动画,因为该对象已被密封或冻结.
要更正此消息,请将控件的背景分配给非冻结实例.
// Do not use a frozen instance
this.elGrid.Background = new SolidColorBrush(Colors.Orange);
this.elGrid.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);
Run Code Online (Sandbox Code Playgroud)
小智 8
ColorAnimation colorChangeAnimation = new ColorAnimation();
colorChangeAnimation.From = VariableColour;
colorChangeAnimation.To = BaseColour;
colorChangeAnimation.Duration = timeSpan;
PropertyPath colorTargetPath = new PropertyPath("(Panel.Background).(SolidColorBrush.Color)");
Storyboard CellBackgroundChangeStory = new Storyboard();
Storyboard.SetTarget(colorChangeAnimation, BackGroundCellGrid);
Storyboard.SetTargetProperty(colorChangeAnimation, colorTargetPath);
CellBackgroundChangeStory.Children.Add(colorChangeAnimation);
CellBackgroundChangeStory.Begin();
Run Code Online (Sandbox Code Playgroud)
// VariableColour&BaseColour是Color类,timeSpan是TimeSpan类,BackGroundCellGrid是Grid类;
//无需在XAML中创建SolidColorBrush并绑定到它; //玩得开心!