Introduction to the WPF interaction Framework (i)--behavior

Source: Internet
Author: User

In WPF 4.0, a more practical library,--interactions, was introduced to inject some new functionality into the UI controls with additional properties, which provided a good extension interface in addition to a series of useful features. Here is a brief introduction to this extension of behavior.

As the name implies, behavior can give the control a new ability to behave, for example, by mousedragelementbehavior the ability to add support for drag-and-drop to a control. Use the following methods:

    1. Add a reference to the interactions library. The main additions are the following two DLL:Microsoft.Expression.Interactions.dll and System.Windows.Interactivity.dll.
    2. Add the following namespaces

      xmlns:i="Clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"
      xmlns:ei="Http://schemas.microsoft.com/expression/2010/interactions"

    3. Adding Mousedragelementbehavior to a control
 <  image  source  = "2.jpg"  >  <  i:interaction  . Behaviors  >  <  ei:mousedragelementbehavior  />   i:interaction. Behaviors  >  </ image  >  

The previous steps in these three steps are to add support for the interactions Library, which is the same for the trigger and action described later, only <ei:mousedragelementbehavior/> a sentence is related to behavior. In fact, we can simplify this process by simply dragging and dropping the mousedragelementbehavior onto the control in blend. After adding Mousedragelementbehavior, our control supports the mouse to drag and move, very force.

In fact, the system also provides a series of very useful behavior, and later I write a separate article to introduce it.

Write your own behavior.

In addition to the behavior provided by the system itself, we can also implement custom behavior by writing behavior ourselves, a simple example of which is as follows:

    classSkewbehavior:behavior<uielement>{SkewTransform _transform; protected Override voidonattached () {Base.            Onattached (); _transform=NewSkewTransform (); Associatedobject.rendertransform=_transform; Associatedobject.rendertransformorigin=NewPoint (0.5,0.5); _transform.anglex= -; }        protected Override voidondetaching () {_transform.anglex=0; Base.        Ondetaching (); }    }
View Code

The code above also implements a behavior that tilts the control horizontally 30 degrees (which is relatively simple and imperfect), with the following three key points:

    1. Gets the attached object through the Associatedobject property.
    2. Initialize operation when behavior is attached by overloading the onattached function
    3. To remove behavior by overloading the Ondetaching function

Although we can do this directly with attached properties, the interactions framework undoubtedly regulates and simplifies this behavior.

Finally, attach a more commonly used mouse drag-and-drop behavior, and the built-in Mousedragelementbehavior is different, it produces mouse events that are used to implement some custom drag-and-drop operations:

    classDragdropbehavior:behavior<uielement>    {         Public EventEventhandler<dragdeltaeventargs>DragDelta;  Public EventEventhandler<eventargs>Drop;        IInputElement _parent; protected Override voidonattached () {Base.            Onattached (); _parent= Logicaltreehelper.getparent (Associatedobject) asIInputElement; if(_parent = =NULL)                return; Associatedobject.mouseleftbuttondown+=OnMouseDown; Associatedobject.mousemove+=OnMouseMove; Associatedobject.mouseleftbuttonup+=onMouseUp; Associatedobject.mouseenter+=OnDragEnter; }        protected Override voidondetaching () {Associatedobject.mouseleftbuttondown-=OnMouseDown; Associatedobject.mousemove-=OnMouseMove; Associatedobject.mouseleftbuttonup-=onMouseUp; Associatedobject.mouseenter-=OnDragEnter; Base.        Ondetaching (); } Point?start; Private voidOnMouseDown (Objectsender, MouseButtonEventArgs e) {Start=mouse.getposition (_parent); }        Private voidOnMouseMove (Objectsender, MouseEventArgs e) {            if(!start. HasValue)return; varp =mouse.getposition (_parent); varoffset = P-start.            Value; Start=p; DragDelta?. Invoke (Associatedobject,NewDragDeltaEventArgs (offset. X, offset.        Y)); }        Private voidOnMouseUp (Objectsender, MouseButtonEventArgs e)        {Tryenddrag (); }        Private voidOnDragEnter (Objectsender, MouseEventArgs e)        {Tryenddrag (); }        voidTryenddrag () {if(Mouse.leftbutton! =mousebuttonstate.released)return; Start=NULL; Drop?.        Invoke (Associatedobject, eventargs.empty); }    }
View Code

Introduction to the WPF interaction Framework (i)--behavior

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.