开发者

How to change the direction of wpf marquee dynamically?

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 ==开发者_C百科 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜