J R B
J R B

Reputation: 2136

Unable to find child element from XAML

I have a UserControl and trying to find (using FindResource) of DoubleAnimation element in code behind. Examp:-

<UserControl ....
   <Canvas Width="400" Height="400" Loaded="Canvas_Loaded">
    <Canvas.Resources>
        <Storyboard x:Key="sd" x:Name="sBoard ">
            <DoubleAnimation x:Name="SomeAnimation" ...

I am trying to find “SomeAnimation” in Canvas_Loaded method.

Please help

Upvotes: 0

Views: 304

Answers (1)

Omri Btian
Omri Btian

Reputation: 6547

FindResource method expect a resource key which SomeAnimation doesn't have. You can use it to find the Storyboard resource with using the sd key and find your animation from there.

private void Canvas_Loaded(object sender, RoutedEventArgs e)
{
    var canvas = sender as Canvas;

    var storyboard = canvas.FindResource("sd") as Storyboard;
    var someAnimation = storyboard.Children.First() as DoubleAnimation;
}

If you do that in order to activate the animation you can do it using BeginStoryboard method

var storyboard = canvas.FindResource("sd") as Storyboard;
canvas.BeginStoryboard(storyboard);

or simply

storyboard.Begin();

Hope this helps

Upvotes: 1

Related Questions