Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
C#
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);
}


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.
Posted
Updated 14-Mar-13 2:38am
v2

1 solution

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

C#
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);
}


By the way, I ended up using the BrushAnimation class from one of the links provided.

I hope this post ends up helping others.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900