在数据模板中设置 StoryBoard 目标
我想使用 StoryBoard 在单击按钮时显示带有平面投影动画的图像。
当我仅在一个实例上尝试时,这是有效的。
但在我的 silverlight 页面(Windows Phone 7)中,我使用数据模板从对象集合中重复它。
而在这里,它不起作用。
这是数据模板的.xaml:
<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>
这是故事板的.xaml(在电话页面资源中):
<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>
这是按钮上单击事件的.cs:
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();
}
但我收到错误:
Cannot resolve TargetName MyImg.
我认为这是因为有多个图像具有 x:Name“MyImg”的对象,但我不知道如何在数据模板中的正确图像上设置故事板目标。
I would like to use a StoryBoard to show an image with plane projection animation when I click on a button.
That works when I try it on a only one instance.
But in my silverlight page (windows phone 7), I use data template to repeat it from a collection of objects.
And here, it doesn't work.
Here is the .xaml of the 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 of the storyboard (in 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 .cs of click event on the 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 error :
Cannot resolve TargetName MyImg.
I think it's because there is multiple Image objects with x:Name "MyImg", but I don't know how to set storyboard target on the correct image in my data template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它无法解析为该名称,因为该名称是 DataTemplate 的本地名称。您可以在 DataTemplate 中移动 Storyboard,以便将其应用于每个实例中的图像,并使用 VisualStateManager 在按下状态下启动动画,或者您可以在代码中创建 Storyboard 并相应地设置目标。
It can't resolve to that name because that name is local to the DataTemplate. You could either move the Storyboard within the DataTemplate so that it can be applied to the image in each instance and use VisualStateManager to start the animation in the Pressed state, or you could create the Storybaord in code and set the target accordingly.