behavior is not the core of WPF, but also the design feature of expression blend. The use behavior can also be replaced by a trigger. However, it is also interesting to use behavior. Let's take a simple example to see its usage.
override the onattached () and ondetaching () methods. Access the elements of placement behavior through associatedobject. Complete the operations of the mouse Drag Control in the event.
Public class mybehavior: behavior <uielement> {private canvas; private bool isdragging = false; private point mouseoffset; protected override void onattached () {base. onattached (); this. associatedobject. mouseleftbuttondown + = new system. windows. input. mousebuttoneventhandler (associatedobject_mouseleftbuttondown); this. associatedobject. mousemove + = new system. windows. input. mouseeventhandler (associatedobject_mousemove); this. associatedobject. mouseleftbuttonup + = new system. windows. input. mousebuttoneventhandler (associatedobject_mouserightbuttonup);} protected override void ondetaching () {base. ondetaching (); this. associatedobject. mouseleftbuttondown-= new system. windows. input. mousebuttoneventhandler (associatedobject_mouseleftbuttondown); this. associatedobject. mousemove-= new system. windows. input. mouseeventhandler (associatedobject_mousemove); this. associatedobject. mouseleftbuttonup-= new system. windows. input. mousebuttoneventhandler (associatedobject_mouserightbuttonup);} void associatedobject_mouserightbuttonup (Object sender, system. windows. input. mousebuttoneventargs e) {If (isdragging) {associatedobject. releasemousecapture (); isdragging = false ;}} void associatedobject_mousemove (Object sender, system. windows. input. mouseeventargs e) {If (isdragging) {point = E. getposition (canvas); associatedobject. setvalue (canvas. topproperty, point. y-mouseoffset. y); associatedobject. setvalue (canvas. leftproperty, point. x-mouseoffset. x) ;}} void associatedobject_mouseleftbuttondown (Object sender, system. windows. input. mousebuttoneventargs e) {If (this. canvas = NULL) {canvas = visualtreehelper. getparent (this. associatedobject) as canvas;} isdragging = true; mouseoffset = E. getposition (associatedobject); associatedobject. capturemouse ();}}
the most important thing is to use behavior on the interface.
generally, blend should be installed for WPF development. (Take the default installation path as an example) You must reference system. Windows. interactivity. dll under c: \ Program Files \ microsoft sdks \ expression \ blend 3 \ interactivity \ libraries \ WPF. Use:
<Window X: class = "testbehavior. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: ibehavior =" CLR-namespace: system. windows. interactivity; Assembly = system. windows. interactivity "xmlns: Local =" CLR-namespace: testbehavior "Title =" mainwindow "Height =" 466 "width =" 875 "> <grid> <canvas> <! -- No use behavior --> <rectangle canvas. left = "10" canvas. top = "10" fill = "yellow" width = "40" Height = "60"> </rectangle> <! -- Use behavior --> <ellipse canvas. left = "10" canvas. top = "70" fill = "blue" width = "80" Height = "60"> <ibehavior: interaction. behaviors> <local: mybehavior> </ibehavior: interaction. behaviors> </ellipse> <! -- Use behavior --> <ellipse canvas. left = "80" canvas. top = "70" fill = "orangered" width = "40" Height = "70"> <ibehavior: interaction. behaviors> <local: mybehavior> </ibehavior: interaction. behaviors> </ellipse> </canvas> </GRID> </WINDOW>
This is done. Press and hold the left mouse button to drag the interface icon.
Code
http://download.csdn.net/detail/yysyangyangyangshan/5422719