Page 1 of 1

Can't get XAML Storyboard by x:Key, have to add x:Name manually

Posted: 01 Jun 2017, 13:48
by Hansi
I found out that I can't use FindName() with the x:Key value of a Storyboard created in BLEND.
When I add a x:Name manually, I can use FindName() to access it.

Is there a way to access a Storyboard via x:Key? BLEND denotes it as "Name", but in the file it's x:Key.
So I'm forced to edit the XAML by hand and add x:Name.

I guess that's not the way it's supposed to be...

Thanks!

Re: Can't get XAML Storyboard by x:Key, have to add x:Name manually

Posted: 01 Jun 2017, 16:40
by sfernandez
I guess you are defining the Storyboard as a resource, something like this:
<Grid x:Name="LayoutRoot">
  <Grid.Resources>
    <Storyboard x:Key="anim".../>
  </Grid.Resources>
  ...
</Grid>
In this case the correct way to access the storyboard is by using the FindResource method (on any element in the UI tree), or directly looking into the resources property (of the layout root element):
var anim = (Storyboard)element.FindResource("anim");
var anim = (Storyboard)layoutRoot.Resources["anim"];

Re: Can't get XAML Storyboard by x:Key, have to add x:Name manually

Posted: 02 Jun 2017, 13:03
by Hansi
Oh nice, didn't think of that.
Thank you so much!

Re: Can't get XAML Storyboard by x:Key, have to add x:Name manually

Posted: 05 Jun 2017, 16:29
by sfernandez
Marked this as solved.