Click here to Skip to main content
16,006,605 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: TabControl: WinForms Vs WPF Pin
User 2710097-May-08 13:29
User 2710097-May-08 13:29 
AnswerRe: TabControl: WinForms Vs WPF Pin
Ajay.k_Singh9-May-08 12:13
Ajay.k_Singh9-May-08 12:13 
GeneralRe: TabControl: WinForms Vs WPF Pin
yanairon11-May-08 1:12
yanairon11-May-08 1:12 
QuestionHow to develop 3D virtual environment online using C#.net? Pin
katelva3-May-08 16:38
katelva3-May-08 16:38 
AnswerRe: How to develop 3D virtual environment online using C#.net? Pin
Michael Sync5-May-08 17:09
Michael Sync5-May-08 17:09 
QuestionHow to develop virtual environment online using C# with Managed DirectX? Pin
katelva3-May-08 8:21
katelva3-May-08 8:21 
AnswerRe: How to develop virtual environment online using C# with Managed DirectX? Pin
Christian Graus3-May-08 13:31
protectorChristian Graus3-May-08 13:31 
QuestionReusable Animation Class Pin
Jammer2-May-08 7:32
Jammer2-May-08 7:32 
Hi All,

I'm still very much a newbie to a lot of C# / WPF and i'm having a bit of trouble making this 'generic' animation type work for me, but I also can't see what the problem is. I've been looking at the code for what already feels like a lifetime and I've gotten nowhere so I'm here now asking silly questions. Sorry for the stack of code below btw.

When I do this:

public void FadeElement(string ElementName, double currentOpacity, double targetOpacity)
{
    Storyboard Fade = new Storyboard();
    TimeSpan duration = new TimeSpan(0, 0, 0, 0, 500); // duration = 500 milliseconds
    DoubleAnimation animation = new DoubleAnimation();
    animation.From = currentOpacity;
    animation.To = targetOpacity;
    animation.Duration = new Duration(duration);
    Storyboard.SetTargetName(animation, ElementName);
    Storyboard.SetTargetProperty(animation, new PropertyPath(Control.OpacityProperty));
    Fade.Children.Add(animation);
    Fade.Begin(this);
}


All works fine.

But I wasn't to 'wrap' this up in a type in a separate library in order to enable me to re-use over and over without having this method in every app I/we make so I did this:

//
// JAG 02/05/2008
//

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Utils.HelperClasses.WPF
{
    /// <summary>
    /// A class to create OpacityAnimations.
    /// </summary>
    public sealed class OpacityAnimation : Storyboard
    {
        #region Fields and Properties

        private Storyboard _storyboard;

        public string ElementName;

        public double CurrentOpacity;

        public double TargetOpacity;

        public OpacityAnimation()
        {
        }

        public Storyboard CreateOpacityAnimation()
        {
            _storyboard = FadeElement(ElementName, CurrentOpacity, TargetOpacity);
            return _storyboard;
        }

        private Storyboard FadeElement(string elementName, double currentOpacity, double targetOpacity)
        {
            Storyboard Fade = new Storyboard();
            TimeSpan duration = new TimeSpan(0, 0, 0, 0, 500); // duration = 500 milliseconds
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = currentOpacity;
            animation.To = targetOpacity;
            animation.Duration = new Duration(duration);
            Storyboard.SetTargetName(animation, elementName);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Control.OpacityProperty));
            Fade.Children.Add(animation);
            return Fade;
        }

        #endregion
    }
}


but then running this code does nothing but as far as I can tell its more or less the same as doing just the method in the first example with some code abstracted away in the other library:

private void button1_Click(object sender, RoutedEventArgs e)
{
    OpacityAnimation oa = new OpacityAnimation();
    oa.CurrentOpacity = 1;
    oa.ElementName = "button2";
    oa.TargetOpacity = 0.3;
    oa.CreateOpacityAnimation();
    oa.Begin(this);
}


Can anyone offer some insight into what I'm overlooking here?

Thanks,

Jammer

Going where everyone here has gone before! Smile | :)
My Blog

AnswerRe: Reusable Animation Class [modified] Pin
Insincere Dave2-May-08 11:42
Insincere Dave2-May-08 11:42 
GeneralRe: Reusable Animation Class Pin
Jammer2-May-08 12:53
Jammer2-May-08 12:53 
GeneralRe: Reusable Animation Class Pin
Jammer2-May-08 13:15
Jammer2-May-08 13:15 
QuestionExpander Control Pin
Jammer2-May-08 4:24
Jammer2-May-08 4:24 
AnswerRe: Expander Control Pin
Jammer2-May-08 4:35
Jammer2-May-08 4:35 
QuestionError on Starting Sliver lights in VS 2008 Pin
Abhijit Jana2-May-08 0:25
professionalAbhijit Jana2-May-08 0:25 
AnswerRe: Error on Starting Sliver lights in VS 2008 Pin
Abhijit Jana2-May-08 7:36
professionalAbhijit Jana2-May-08 7:36 
NewsSource and Unit Tests available now for Silverlight 2 Beta 1 Controls Pin
brucedkyle1-May-08 11:49
brucedkyle1-May-08 11:49 
GeneralRe: Source and Unit Tests available now for Silverlight 2 Beta 1 Controls Pin
Michael Sync5-May-08 16:47
Michael Sync5-May-08 16:47 
QuestionTargetInvocationException [modified] Pin
Ian Grech1-May-08 10:39
Ian Grech1-May-08 10:39 
AnswerRe: TargetInvocationException Pin
Edmundisme1-May-08 10:51
Edmundisme1-May-08 10:51 
GeneralRe: TargetInvocationException Pin
Ian Grech1-May-08 11:08
Ian Grech1-May-08 11:08 
QuestionProperly display dynamic calculations in databinding? [modified] Pin
artwallacex30-Apr-08 12:15
artwallacex30-Apr-08 12:15 
AnswerRe: Properly display dynamic calculations in databinding? Pin
Pete O'Hanlon30-Apr-08 12:41
mvePete O'Hanlon30-Apr-08 12:41 
GeneralRe: Properly display dynamic calculations in databinding? [modified] Pin
artwallacex30-Apr-08 12:54
artwallacex30-Apr-08 12:54 
GeneralRe: Properly display dynamic calculations in databinding? Pin
User 27100930-Apr-08 15:07
User 27100930-Apr-08 15:07 
GeneralRe: Properly display dynamic calculations in databinding? Pin
artwallacex30-Apr-08 15:21
artwallacex30-Apr-08 15:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.