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:
- Add a reference to the interactions library. The main additions are the following two DLL:Microsoft.Expression.Interactions.dll and System.Windows.Interactivity.dll.
- Add the following namespaces
xmlns:i="Clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity"
xmlns:ei="Http://schemas.microsoft.com/expression/2010/interactions"
- 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:
- Gets the attached object through the Associatedobject property.
- Initialize operation when behavior is attached by overloading the onattached function
- 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