Example tutorials for customizing gridlengthanimation in WPF

Source: Internet
Author: User
This article is mainly for you to introduce in detail the WPF custom gridlengthanimation related Materials, with a certain reference value, interested in small partners can refer to

Demand

We want to edit an item in a list by placing the details of the edits in the current face, such as the right side.

This requirement can be achieved by dynamically adjusting the width of two cloumn by dividing a grid into two cloumn.

We know that the width of the Clomun is a, and the default animation does not have such a child. We need to make such a human animation ourselves.

Design
We see it from the Animation class diagram.

We can from the demand

We want to edit an item in a list by placing the details of the edits in the current face, such as the right side.

This requirement can be achieved by dynamically adjusting the width of two cloumn by dividing a grid into two cloumn.

We know that the width of Clomun is a gridlength, and the default animation does not. We need to make such a human animation ourselves.

Design

We see AnimationTimeline inheritance from the animation class diagram, overriding its getcurrentvalue

public class Gridlengthanimation:animationtimeline {//<summary>//Returns The type of object to Animat     E///</summary> public override Type Targetpropertytype = typeof (GridLength); <summary>//Creates an instance of the animation object///</summary>//<returns>retur NS The instance of the Gridlengthanimation</returns> protected override System.Windows.Freezable Createinstanceco    Re () {return new gridlengthanimation (); }///<summary>//Dependency property for the From property///</summary> public static Readon ly DependencyProperty Fromproperty = Dependencyproperty.register ("from", typeof (GridLength), typeof (Gridlengthanimat     ION)); <summary>//CLR Wrapper for the From Depenendency property//</summary> public GridLength Fro      m {get {return (GridLength) GetValue (Gridlengthanimation.fromproperty); } SEt {SetValue (gridlengthanimation.fromproperty, value); }}///<summary>//Dependency property for the property///</summary> public static Rea DOnly DependencyProperty toproperty = Dependencyproperty.register ("to", typeof (GridLength), typeof (Gridlengthanimati     ON));      <summary>//CLR Wrapper for the property///</summary> public GridLength to {get      {return (GridLength) GetValue (Gridlengthanimation.toproperty);      } set {SetValue (Gridlengthanimation.toproperty, value); }}///<summary>//Animates the grid let set//</summary>//<param name= "Defaultorig Invalue ">the original value to animate</param>//<param name=" Defaultdestinationvalue ">the final value </param>//<param name= "AnimationClock" >the animation clock (timer) </param>//<returns>r Eturns the new grid length to Set</returns> public override Object GetCurrentValue (object defaultOriginValue, Object Defaultdestinationva Lue, AnimationClock animationclock) {Double fromval = ((GridLength) GetValue (Gridlengthanimation.fromproperty)).       Value; Double Toval = ((GridLength) GetValue (Gridlengthanimation.toproperty)).       Value; if (Fromval > Toval) return new GridLength ((1-animationclock.currentprogress.value) * (fromval-toval) + toVa      L, Gridunittype.star);    else return new GridLength (AnimationClock.CurrentProgress.Value * (toval-fromval) + Fromval, Gridunittype.star); }

As shown above, we implement from,to with the default animation and define its properties as gridlength, and when the animation executes, we rewrite the GetCurrentValue to associate it with the from/to attribute.

Optimization

Through the above code, we implemented the animation when the gridlength changes. However, after the trial we found that the animation was a little too linear. This time, what to do?

Can be achieved by introducing easingfunction. We know that easingfunction is actually a time function f (t) that is related to time t. Through the processing of time functions, we make the animation transition not so linear.


///<summary>//The <see cref= "Easingfunction"/> dependency property ' s name.     </summary> Public Const string easingfunctionpropertyname = "Easingfunction"; <summary>/Sets the value of the <see cref= "easingfunction"/>//property.    This was a dependency property. </summary> public ieasingfunction easingfunction {get {return (ieasingfunction) GetValue      (Easingfunctionproperty);      } set {SetValue (Easingfunctionproperty, value);    }}///<summary>//Identifies the <see cref= "Easingfunction"/> dependency property.      </summary> public static readonly DependencyProperty Easingfunctionproperty = Dependencyproperty.register ( Easingfunctionpropertyname, typeof (Ieasingfunction), typeof (Gridlengthanimation), new UIPropertyMetadata (null)); 

Also, rewrite the GetCurrentValue function.


public override Object GetCurrentValue (object defaultOriginValue,      object Defaultdestinationvalue, AnimationClock AnimationClock)    {      Double fromval = ((GridLength) GetValue (Fromproperty)). Value;       Double Toval = ((GridLength) GetValue (Toproperty)). Value;       Check the from is set from the caller      //if (fromval = = 1)////set the From as the  actual value      //
  
   fromval = ((GridLength) defaultdestinationvalue). Value;       Double progress = animationClock.CurrentProgress.Value;       Ieasingfunction easingfunction = easingfunction;      if (easingfunction! = null)      {        progress = easingfunction.ease (progress);      }        if (Fromval > Toval)        return new GridLength ((1-progress) * (fromval-toval) + Toval, Gridunittype.star);         return new GridLength (Progress * (Toval-fromval) + Fromval, Gridunittype.star);    }
  

Use

<anim:gridlengthanimation storyboard.targetproperty= "Width" from= "0" to= "*" duration= "0:0:0.5"/>

"Recommended"

1. ASP. NET free Video Tutorials

2. Example of implementing shortcut key customization in C # WinForm, _php tutorial

3. Share the public number develop a custom menu instance tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.