如何在代码中为WPF画笔对象设置动画(C#)
我正在创建一个类似于WPF的HeaderedContentControl的UserControl,其行为略显复杂。我想在我的控件中定义一个名为State的枚举器 - 目前填充了一些简单的值(即Default,Normal,Warning,Error)。接下来,我将使用PropertyChangedCallback创建一个名为CurrentState的DependencyProperty(DP)类型状态。当CurrentState改变时,我想在控件上的某个边框的背景颜色也改变颜色/画笔。
目标颜色/画笔由a定义使用公共访问修饰符的其他几个DP(即NormalStateBrush,WarningStateBrush和ErrorStateBrush),以便此控件的使用者可以决定状态应转换为什么颜色(或任何其他类型的画笔对象)。
我所针对的动画并不一定非常复杂(例如500ms以上的简单淡入就足够了)但是对于警告状态和错误状态,我希望要重复的动画 - 创建一个有点发光的效果,而动画完成时应该停止转换到正常状态。
我试过简单地使用WPF' VisualState构造(VisualStateManager等)以及正常,警告和错误的硬编码颜色 - 在功能上是可接受的,但颜色控制消费者无法改变。
现在,这就是我所说的第二次尝试或版本2.
以下是State / CurrentState / DP定义到目前为止:
Hi, I''m creating a UserControl similar to WPF''s HeaderedContentControl with a slightly complicated behavior. I would like to define an enumerator within my control called "State" - at the moment populated with a few simple values (ie. Default, Normal, Warning, Error). Next I would create a DependencyProperty (DP) typed State called CurrentState, with a PropertyChangedCallback. When CurrentState is changed, I would like a background color of a certain border on the control to change color/brush also.
The target color/brush is defined by a few additional DPs (namely NormalStateBrush, WarningStateBrush, and ErrorStateBrush) with the public access modifier, so that consumers of this control can decide what color (or any other type of brush object) the state should transition to.
The animation I am targeting doesn''t have to be complex, (for example a simple fade-in over 500ms is sufficient) however for Warning-state and Error-state, I would like the animation to repeat - creating a somewhat glowing effect, whereas a transition to Normal-state should stop when the animation completes.
I have tried doing this simply by using WPF''s VisualState constructs (VisualStateManager, etc.) along with hard-coded colors for Normal, Warning, and Error - which functionally is acceptable, but the colors can''t get changed by control consumers.
So now, this is what I call the second attempt or version 2.
Below is what I have so far for the State/CurrentState/DP definition:
public enum State
{
Default, Normal, Warning, Error
}
[Bindable(true), Category("Common Properties")]
public State CurrentState
{
get { return (State)GetValue(CurrentStateProperty); }
set { SetValue(CurrentStateProperty, value); }
}
public static readonly DependencyProperty CurrentStateProperty = DependencyProperty.Register("CurrentState", typeof(State), typeof(ColorStateButton2nd), new UIPropertyMetadata(State.Default, CurrentStateProperty_Changed));
static void CurrentStateProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var c = d as ColorStateButton2nd;
var s = (State)e.NewValue;
c.ChangeCurrentState(s, true);
}
internal void ChangeCurrentState(State state, bool useTransitions)
{
Storyboard sb = new Storyboard();
sb.Duration = TimeSpan.FromMilliseconds(300);
switch (state)
{
case State.Default:
break;
case State.Normal:
sb.SetValue(CurrentStateColorProperty, NormalColorBrush);
break;
case State.Warning:
sb.SetValue(CurrentStateColorProperty, WarningColorBrush);
break;
case State.Error:
sb.SetValue(CurrentStateColorProperty, ErrorColorBrush);
break;
}
sb.BeginAnimation(CurrentStateColorProperty, null);
}
编辑*某个边框对象的BackgroundProperty只是绑定到此CurrentStateColorProperty
此外,我已经审阅了网上的一些相关帖子;但没有任何描述的情况与我的情况完全相同:
以编程方式控制故事板 - http://social.msdn.microsoft.com/forums/en-US/wpf/thread/50041bf3-f669-41d2- a7ee-1651a533933a / [ ^ ]
LinearGradientBrushAnimation - LinearGradientBrushAnimation [ ^ ]
Animate矩形填充 - http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb59213d -e469-4979-b424-286f1c881cce [ ^ ]
在WPF中组合画笔 - http://stackoverflow.com/questions/1542274/how-do-i-combine-brushes-in-wpf [ ^ ]
任何帮助或想法都会很棒,甚至只是指出正确的方向会很愉快。
谢谢。
Edit* The BackgroundProperty of that certain border object is simply bound to this CurrentStateColorProperty
Additionally, I have already reviewed some of the related posts from the web; but nothing describes a situation quite like the one I have:
Programatically control a Storyboard - http://social.msdn.microsoft.com/forums/en-US/wpf/thread/50041bf3-f669-41d2-a7ee-1651a533933a/[^]
LinearGradientBrushAnimation - LinearGradientBrushAnimation[^]
Animate Rectangle Fill - http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fb59213d-e469-4979-b424-286f1c881cce[^]
combine brushes in WPF - http://stackoverflow.com/questions/1542274/how-do-i-combine-brushes-in-wpf[^]
Any help or ideas would be great, or even just pointing me the right direction would be delightful.
Thanks.
OMG,经过一些修补......似乎我找到了解决自己问题的方法。这只是一种可能的解决方案;如果可能的话,请提供意见。谢谢
OMG, after some tinkering... it seems I have found a solution to my own problem. Here is but one possible solution; still please provide comments if possible. Thanks
internal void ChangeCurrentState(State state)
{
BrushAnimation ba = new BrushAnimation();
ba.Duration = TimeSpan.FromMilliseconds(300);
ba.From = CurrentStateColor;
switch (state)
{
case State.Default:
break;
case State.Normal:
ba.To = NormalColorBrush;
break;
case State.Warning:
ba.To = WarningColorBrush;
break;
case State.Error:
ba.To = ErrorColorBrush;
break;
}
border.BeginAnimation(Border.BackgroundProperty, ba);
}
顺便说一句,我最终使用了提供的链接之一的BrushAnimation类。
我希望这篇文章能够帮助其他人。
By the way, I ended up using the BrushAnimation class from one of the links provided.
I hope this post ends up helping others.