c# - 在数据模板中设置 StoryBoard 目标

标签 c# xaml windows-phone-7 datatemplate 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>

这是 Storyboard的 .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”的图像对象,但我不知道如何在我的数据模板中的正确图像上设置 Storyboard目标。

最佳答案

它无法解析为该名称,因为该名称是 DataTemplate 的本地名称。您可以在 DataTemplate 中移动 Storyboard,以便它可以应用于每个实例中的图像,并使用 VisualStateManager 在 Pressed 状态下启动动画,或者您可以在代码中创建 Storybaord 并相应地设置目标。

关于c# - 在数据模板中设置 StoryBoard 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5496153/

相关文章:

c# - 序列化为 XML 时添加属性

c# - 如何访问网站客户端的内容?

c# - 外键异常

c# - Excel 互操作宏输出/引用参数

c# - 在 WPF 中。如何像iPhone一样通过鼠标拖动滚动ScrollViewer中的对象?

c# - Silverlight:来自一个用户控件的事件能否在不同的用户控件和/或包含控件上开始动画

silverlight - 我可以更改 ItemTemplate 中 DataTemplate 的 VisualState 吗?

wpf - 多个绑定(bind) ViewModel 到 View MVVM

c# - MVVM:哪个组件负责导航?

windows-phone-7 - 在Windows Phone媒体库中唯一标识专辑和歌曲