-2

我有控制模板:

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");

但动画不起作用。

4

1 回答 1

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);
于 2013-06-12T13:04:38.033 回答