0

私のコードはこんな感じです。グリッドを動的に作成したい。作成は成功ですが、色をアニメーション化できません。私の間違いは何ですか?

        Grid[] grid = new Grid[99];
        for (int i = 0; i < 10; i++) {
            grid[i] = new Grid();
            grid[i].Width = grid[i].Height = 100;
            grid[i].Background = Brushes.WhiteSmoke;

            Storyboard sb = new Storyboard();
            ColorAnimation ca = new ColorAnimation(Colors.DarkTurquoise, TimeSpan.FromMilliseconds(250));
            Storyboard.SetTarget(ca, grid[i]);
            Storyboard.SetTargetProperty(ca, new PropertyPath("Fill.Color"));
            sb.Children.Add(ca);
            grid[i].MouseEnter += delegate(object sender2, MouseEventArgs e2) {
                sb.Begin(this);
            };

            stackMain.Children.Add(grid[i]);
        }
4

2 に答える 2

4

WPFには、使用する必要があるプロパティGridはありませんFillBackground

 Storyboard.SetTargetProperty(ca, new PropertyPath("Background.Color"));
于 2013-01-16T04:55:51.163 に答える
1

sa_ddam が言ったことに加えて、組み込みのブラシ (例のように) をアニメーション化できないため、ブラシも作成する必要があります。Brushes.WhiteSmoke

grid[i].Background = new SolidColorBrush(Colors.WhiteSmoke);
...

Storyboard.SetTargetProperty(ca, new PropertyPath("Background.Color"));

ストーリーボードを省略してアニメーションを直接実行すると、コードを節約できる場合もあります。

var brush = new SolidColorBrush(Colors.WhiteSmoke);
grid[i].Background = brush;

var ca = new ColorAnimation(Colors.DarkTurquoise, TimeSpan.FromMilliseconds(250));

grid[i].MouseEnter +=
    (o, e) => brush.BeginAnimation(SolidColorBrush.ColorProperty, ca);
于 2013-01-16T09:25:21.317 に答える