tags:

views:

92

answers:

1

Hi all, I want to change the direction of my marquee on changeDirection button click. My code for changing direction is :

    private void changeDirection_click(object sender, RoutedEventArgs e)
    {
        if (_marqueeType == MarqueeType.RightToLeft)
        {
            _marqueeType = MarqueeType.LeftToRight;
            StartMarqueeing(_marqueeType);
        }
        else if (_marqueeType == MarqueeType.LeftToRight)
        {
            _marqueeType = MarqueeType.RightToLeft;
            StartMarqueeing(_marqueeType);
        }
    }

And code for start marquee is :

public void StartMarqueeing(MarqueeType marqueeType)
    {

        double height = canMain.ActualHeight - marqueeList.ActualHeight;
        marqueeList.Margin = new Thickness(0, 0, 0, 0);            
        doubleAnimation.From = -marqueeList.ActualWidth;
        doubleAnimation.To = canMain.ActualWidth;
        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));

        if (marqueeType == MarqueeType.RightToLeft)
        {
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Right)"));
            _storyBoard.Children.Add(doubleAnimation);
            _storyBoard.Begin(marqueeList, true); 
        }
        else if (marqueeType == MarqueeType.LeftToRight)
        {
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Left)"));
            _storyBoard.Children.Add(doubleAnimation);
            _storyBoard.Begin(marqueeList, true); 
        }
    }

Now here I am able to change the direction from Right to Left only first time. But when I am change it from Left to Right it’s not changing the marquee position Left to Right.

Please help me on this...........its urgent!!!!!!!!!!!

+1  A: 

It looks like you left out a _storyboard = new Storyboard(), perhaps at the top of the StartMarqueeing method.

From what I see it appears that every call to StartMarqueeing will add an additional DoubleAnimation to the storyboard, then start it again. So all the old DoubleAnimations will be recreated, and it looks like they take precedence.

Try creating a new Storyboard object each time, not just re-using it and adding to its children collection.

Update

Oh, now I see the problem. You should not be setting both (Canvas.Left) and (Canvas.Right). Use only one of the two: That's all you need anyway, and using both will give the Canvas conflicting instructions. Traditionally people use (Canvas.Left). I think that's what Canvas selects, which is what is causing your asymmetry.

You may wonder why I say you are using both when you don't think your two animations every run at the same time. Actually they do: The first animation runs then holds the value on the animated property until it is removed or bumped off by another animation. If the second animation then runs and modifies a different property it doesn't bump off the first animation so the first animation's value is still present.

The bottom line is, using (Canvas.Left) on both animations should fix it as long as you are using the default HandoffBehavior.SnapshotAndReplace.

Ray Burns
Thanks for ur reply..! I tried this but not working..! In my above code I am initializing marquee from right side and on change button click its change it from right to left on first time. But if I initialize marquee from left then it’s not able change it from left to right even first time. That means there is some problem with left side code. :-(((
ashish semwal
Aha! That clued me in. I didn't notice you were setting two different properties. I've added an update to my answer to explain what is going wrong and how to fix it.
Ray Burns
Thanks of ur reply ! But I do not understanding this! If I set both Canvas. Left then how can I change the marquee direction dynamically??
ashish semwal
Change the From and To in your DoubleAnimation instead of changing the TargetProperty on the Storyboard.
Ray Burns
:-)))))) Thanks a lot...!!! i'll give u my this month salary :-)))))) ......but that just jokes a part:-((-------------- thanks again ...!!
ashish semwal