Set StoryBoard Target to datatemplate

I would like to use StoryBoard to show an image with an animation of the projection of the plane when I press the button.

This works when I try to use it in only one instance.

But on my silverlight page (windows phone 7) I use a data template to repeat it from a collection of objects.

And here it does not work.

Here is the .xaml data template:

<DataTemplate x:Name="MyDt"> <Button Tag="{Binding iId}" BorderThickness="0" Click="buttonClick" Width="456" Style="{StaticResource ButtonStyle}"> <Image x:Name="MyImg" Source="Images/image.png" Visibility="Collapsed"> <Image.Projection> <PlaneProjection/> </Image.Projection> </Image> </Button> </DataTemplate> 

Here is the .xaml storyboard (in the phone page resources):

  <Storyboard x:Name="storyboardShowImage"> <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName=""> <EasingDoubleKeyFrame KeyTime="0" Value="90"/> <EasingDoubleKeyFrame KeyTime="00:00:01" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> 

Here is the button click .cs button:

 private void buttonClick(object sender, RoutedEventArgs e) { /* Get image object from x:Name */ Image img; img = GetChildImage((DependencyObject)sender, "MyImg"); /* Launch storyboard on img object */ ((DoubleAnimationUsingKeyFrames)storyboardShowImage.Children[0]).SetValue(Storyboard.TargetNameProperty, img.Name); storyboardShowImage.Begin(); } 

But I get the error:

 Cannot resolve TargetName MyImg. 

I think this is because there are several Image objects with x: Name "MyImg", but I do not know how to set the storyboard goal to the correct image in my data template.

+4
source share
1 answer

It cannot resolve this name because this name is local to the DataTemplate. You can either move the storyboard to a DataTemplate so that it can be applied to the image in each instance, and use the VisualStateManager to start the animation in the Press state, or you could create a Storybaord in the code and adjust the target accordingly.

+3
source

Source: https://habr.com/ru/post/1345995/


All Articles