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

本文关键字:添加 动画 UIElement 何访问 访问 | 更新日期: 2023-09-27 18:33:30

我的XAML中有一个包含一些UserControl的统一网格。每个用户控件都有一个椭圆。O 尝试访问椭圆以添加动画,但无法获得制服网格的孩子。

谢谢。

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(添加子项)-> Cell 是对象用户控件:

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

其他.cs :当我尝试以这种方式访问孩子(最后一行)时。棋盘是UI制服网格的名称,我不知道如何渲染动画。当我尝试从名称棋盘切换到制服网格时,它正在处理这个 uiElement,但我想访问棋盘上包含的椭圆(子级):

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()));
        } 

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

这似乎是您所需要的:

void animation(CellControl cell)
{
    var animation = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(1.0));
    cell.ellipse.BeginAnimation(UIElement.OpacityProperty, animation);
}