如何访问UIElement的子项以添加动画

时间:2014-12-07 21:12:05

标签: c# wpf get children uielement

我在我的XAML中有一个包含一些UserControl的uniformgrid。每个UserControl都有一个Ellipse。我试图访问椭圆来添加动画,但不可能得到uniformgrid的孩子。

谢谢。

XAML:

<Grid Grid.Column="1" Grid.Row="2">
        <Separator Opacity="0" Height="20"/>

        <!-- Checkerboard -->
        <!-- ************ -->
        <UniformGrid Name="checkerboard" Height="540" Width="540" HorizontalAlignment="Center" VerticalAlignment="Center"/>

XAML.CS(添加孩子) - &gt; Cell是对象用户控件:

public void addCellToBoard(CellControl cell)
        {
            checkerboard.Children.Add(cell);
        }

Other.cs:当我尝试以这种方式接近孩子(最后一行)时。棋盘格是UI uniformgrid的名称,我不知道如何渲染动画。当我尝试从name checkerBoard rendre到uniformgrid时,那个正在处理这个uiElement但我想访问checkerBoard中包含的Ellipse(子):

private void animation(CellControl cell)
        {
            Storyboard storyboard = new Storyboard();
            TimeSpan duration = new TimeSpan(0, 0, 1);
            DoubleAnimation animation = new DoubleAnimation();
            animation.From = 0.0;
            animation.To = 1.0;
            animation.Duration = new Duration(duration);
            Storyboard.SetTargetName(animation, othelloWindow.checkerboard.Name);
            Storyboard.SetTargetProperty(animation, new PropertyPath(Control.OpacityProperty));
            storyboard.Children.Add(animation);
            storyboard.Begin(othelloWindow.checkerboard.FindName(cell.ellipse.Name.ToString()));

        } 

1 个答案:

答案 0 :(得分:0)

这似乎就是你所需要的:

void animation(CellControl cell)
{
    var animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(1.0));

    cell.ellipse.BeginAnimation(UIElement.OpacityProperty, animation);
}