Search code examples
c#wpfgetchildrenuielement

How to access children of UIElement to add animation


i have in my XAML an uniformgrid who contain some UserControl. Each UserControl avec an Ellipse. O try to access to the Ellipse to add animation but impossible to get children of the uniformgrid.

Thank you.

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 (add children) -> Cell is an object user control:

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

Other.cs : when i try to acess to the children (last line) not possible at this way. Checkerboard is the name of the UI uniformgrid, i don't know how to acess to render the animation. When i try to rendre to uniformgrid from name checkerBoard, that's working on this uiElement but i want to access to the Ellipse (children) included on the checkerBoard :

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

        } 

Solution

  • This seems to be all you need:

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