图像控制模板后面的动画代码

时间:2013-06-12 03:44:28

标签: wpf

我有controltemplate:

Image x:Name="image" Height="100" Width="100"/>
<ContentPresenter x:Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

事件点击后面的代码:

        var btn = sender as MultipointButton;
        var arg = e as MultipointMouseEventArgs;
        Storyboard sb = new Storyboard();
        ObjectAnimationUsingKeyFrames obj = new ObjectAnimationUsingKeyFrames();
        obj.BeginTime = TimeSpan.Zero;
        DiscreteObjectKeyFrame disobj = new DiscreteObjectKeyFrame();
        disobj.KeyTime = TimeSpan.FromSeconds(0.2);
        disobj.Value = CreateBitmapImage(Properties.Resources.SelectAnimation, 0, 0);
        obj.KeyFrames.Add(disobj);
        sb.Children.Add(obj);
        btn.Name = "image";
        this.RegisterName("image", btn);
        Storyboard.SetTargetName(obj, btn.Name);
        Storyboard.SetTargetProperty(obj, new PropertyPath(System.Windows.Controls.Image.SourceProperty));
        sb.Begin(this);
        this.UnregisterName("image");

但动画不起作用。

1 个答案:

答案 0 :(得分:1)

好的,我尝试修复它,感谢Roney Michael。 代码在事件点击中完成:

    var btn = sender as MultipointButton;
    var arg = e as MultipointMouseEventArgs;

    Storyboard sb = new Storyboard();

    ObjectAnimationUsingKeyFrames obj = new ObjectAnimationUsingKeyFrames();

    DiscreteObjectKeyFrame disobj = new DiscreteObjectKeyFrame();
    disobj.KeyTime = TimeSpan.FromMilliseconds(0);
    disobj.Value = CreateBitmapImage(Properties.Resources.SelectAnimation, 0, 0); obj.KeyFrames.Add(disobj);

    obj.KeyFrames.Add(disobj);
    sb.Children.Add(obj);

    System.Windows.Controls.Image image = btn_menu.Template.FindName("image", btn) as System.Windows.Controls.Image;
    this.RegisterName(image.Name, image);

    Storyboard.SetTargetName(obj, image.Name);
    Storyboard.SetTargetProperty(obj, new PropertyPath(System.Windows.Controls.Image.SourceProperty));
    sb.Begin(this);