WPF:以编程方式在网格中设置矩形颜色动画

发布于 2024-12-11 09:21:10 字数 596 浏览 0 评论 0原文

如何以编程方式更改网格中矩形的颜色?

        ColorAnimation myColorAnimation = new ColorAnimation();
        myColorAnimation.From = Colors.Red;
        myColorAnimation.To = Colors.Blue;
        myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
        myColorAnimation.AutoReverse = false;

        myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myColorAnimation);
        Storyboard.SetTargetName(myColorAnimation, ?); // What do I put here
        Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath //What do I put here?

How can I programatically change the Color of a Rectangle in my Grid?

        ColorAnimation myColorAnimation = new ColorAnimation();
        myColorAnimation.From = Colors.Red;
        myColorAnimation.To = Colors.Blue;
        myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
        myColorAnimation.AutoReverse = false;

        myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myColorAnimation);
        Storyboard.SetTargetName(myColorAnimation, ?); // What do I put here
        Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath //What do I put here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2024-12-18 09:21:10

好的,我找到了一种方法来做到这一点:

我为每个网格项创建一个故事板:

    List<Storyboard> _animatableGridRectToGray = new List<Storyboard>();
    List<Storyboard> _animatableGridRectToWhite = new List<Storyboard>();

并填充:

private void initAnimatableGridRectangles()
{
    int index = 1;
    foreach (Rectangle rect in SequenceGrid.Children)
    {
        SolidColorBrush tempBrush = new SolidColorBrush();
        rect.Fill = tempBrush;
        string brushName = "Brush" + index;
        _gridBrushes.Add(brushName, tempBrush);
        this.RegisterName(brushName, tempBrush);

        Storyboard tempSBToGray = new Storyboard();
        Storyboard tempSBToWhite = new Storyboard();
        ColorAnimation tempColAnimToGray = getAnimToGray();
        ColorAnimation tempColAnimToWhite = getAnimToWhite();
        tempSBToGray.Children.Add(tempColAnimToGray);
        tempSBToWhite.Children.Add(tempColAnimToWhite);
        Storyboard.SetTargetName(tempColAnimToGray, brushName);
        Storyboard.SetTargetName(tempColAnimToWhite, brushName);
        Storyboard.SetTargetProperty(tempColAnimToGray, new PropertyPath(SolidColorBrush.ColorProperty));
        Storyboard.SetTargetProperty(tempColAnimToWhite, new PropertyPath(SolidColorBrush.ColorProperty));
        _animatableGridRectToGray.Add(tempSBToGray);
        _animatableGridRectToWhite.Add(tempSBToWhite);
        index++;
    }
}


private ColorAnimation getAnimToGray()
{
    ColorAnimation colAnim = new ColorAnimation();
    colAnim.To = Colors.Gray;
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
    colAnim.AutoReverse = false;
    return colAnim;
}

private ColorAnimation getAnimToWhite()
{
    ColorAnimation colAnim = new ColorAnimation();
    colAnim.To = Colors.White;
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
    colAnim.AutoReverse = false;
    return colAnim;
}

然后我可以像这样制作动画:

_animatableGridRectToGray[index].Begin(this);

OK, I found a way to do this:

I create a StoryBoard for each Grid item:

    List<Storyboard> _animatableGridRectToGray = new List<Storyboard>();
    List<Storyboard> _animatableGridRectToWhite = new List<Storyboard>();

and populate:

private void initAnimatableGridRectangles()
{
    int index = 1;
    foreach (Rectangle rect in SequenceGrid.Children)
    {
        SolidColorBrush tempBrush = new SolidColorBrush();
        rect.Fill = tempBrush;
        string brushName = "Brush" + index;
        _gridBrushes.Add(brushName, tempBrush);
        this.RegisterName(brushName, tempBrush);

        Storyboard tempSBToGray = new Storyboard();
        Storyboard tempSBToWhite = new Storyboard();
        ColorAnimation tempColAnimToGray = getAnimToGray();
        ColorAnimation tempColAnimToWhite = getAnimToWhite();
        tempSBToGray.Children.Add(tempColAnimToGray);
        tempSBToWhite.Children.Add(tempColAnimToWhite);
        Storyboard.SetTargetName(tempColAnimToGray, brushName);
        Storyboard.SetTargetName(tempColAnimToWhite, brushName);
        Storyboard.SetTargetProperty(tempColAnimToGray, new PropertyPath(SolidColorBrush.ColorProperty));
        Storyboard.SetTargetProperty(tempColAnimToWhite, new PropertyPath(SolidColorBrush.ColorProperty));
        _animatableGridRectToGray.Add(tempSBToGray);
        _animatableGridRectToWhite.Add(tempSBToWhite);
        index++;
    }
}


private ColorAnimation getAnimToGray()
{
    ColorAnimation colAnim = new ColorAnimation();
    colAnim.To = Colors.Gray;
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
    colAnim.AutoReverse = false;
    return colAnim;
}

private ColorAnimation getAnimToWhite()
{
    ColorAnimation colAnim = new ColorAnimation();
    colAnim.To = Colors.White;
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500));
    colAnim.AutoReverse = false;
    return colAnim;
}

Then I can animate like so:

_animatableGridRectToGray[index].Begin(this);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文