WPF-17 behavior (take the control drag on the interface as an example)

Source: Internet
Author: User

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

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.