Silverlight smart form implementation code (1/2)

Source: Internet
Author: User

Silverlight smart form implementation code

The drag of the control from the toolbox to the canvas reminds me of the dragdroptarget control in tookit. The toolbox is good to say that the listbox or treeview is used, and the canvas does not know how to do it, so I decided to do the dragdrop by myself.

 

There is a listbox on the left and canvas on the right. Create a class library dragdroplibrary (this name is not good ).

Click listbox on the left, move the mouse to the red part on the right, loosen the mouse control, and place it in the released position. The idea is that simple.

Xaml of listbox :( controls = new string [3] {"textbox", "button", "textblock"}; this is the data for listbox, test ...)

<Listbox x: name = "toolbox" itemssource = "{binding controls, mode = onetime}">
<Listbox. itemtemplate>
<Datatemplate>
<Textblock text = "{binding}" mouseleftbuttondown = "onlistboxitemmouseleftbuttondown"/>
</Datatemplate>
</Listbox. itemtemplate>
</Listbox>

Item is a textblock. We need to register the mouseleftbuttondown event.

Add a class to dragdroplibrary:

Show sourceview sourceprint? 01 using system. windows;

02 using system. windows. controls;

03 using system. windows. input;

04 using system. windows. media;

05 using system. windows. media. imaging;

06

07 namespace dragdroplibrary

08 {

09 public static class dragdropmanage

10 {

11 private static panel _ root, _ board;

12 // drop

13 public delegate void drop (dragdropeventargs target );

14 private static drop _ ondrag;

15 private static dragdropeventargs eveargs;

16

17 // follow effect when dragging the mouse

18 private static image _ mouseeffert;

19

20 /// <summary>

21 /// called before dragging

22 /// </summary>

23 // <param name = "root"> root panel (children includes toolbox and canvas) </param>

24 /// <param name = "board"> canvas </param>

25 public static void register (panel root, panel board)

26 {

27 _ root = root;

28 _ board = board;

29 if (_ root is grid)

30 {

31 grid. setcolumnspan (_ mouseeffert, (_ root as grid). columndefinitions. count + 1 );

32 grid. setrowspan (_ mouseeffert, (_ root as grid). rowdefinitions. count + 1 );

33}

34}

35

36 static dragdropmanage ()

37 {

38 _ mouseeffert = new image ();

39}

40

41 public static void begindrag (object sender, mousebuttoneventargs e, drop)

42 {

43 frameworkelement target = sender as frameworkelement;

44 writeablebitmap bitmap = new writeablebitmap (target, new translatetransform ());

45 _ mouseeffert. source = bitmap;

46 _ mouseeffert. height = bitmap. pixelheight;

47 _ mouseeffert. width = bitmap. pixelwidth;

48 _ root. children. add (_ mouseeffert );

49 point position = e. getposition (_ root );

50 _ mouseeffert. margin = new thickness (position. x, position. y, 0, 0 );

51

52 _ mouseeffert. horizontalalignment = horizontalalignment. left;

53 _ mouseeffert. verticalalignment = verticalalignment. top;

54 _ mouseeffert. capturemouse ();

55 eveargs = new dragdropeventargs (target );

56

57 _ ondrag = drop;

58 _ root. mousemove + = onrootmousemove;

59 _ root. mouseleftbuttonup + = onrootmouseleftbuttonup;

60}

61

62 private static void clear ()

63 {

64 _ root. mousemove-= onrootmousemove;

65 _ root. mouseleftbuttonup-= onrootmouseleftbuttonup;

66 _ root. children. remove (_ mouseeffert );

67 _ ondrag = null;

68 eveargs = null;

69}

70

71 private static void onrootmouseleftbuttonup (object sender, mousebuttoneventargs e)

72 {

73 eveargs. position = e. getposition (_ board );

74 _ ondrag (eveargs );

75 clear ();

76}

77

78 private static void onrootmousemove (object sender, mouseeventargs e)

79 {

80 point position = e. getposition (_ root );

81 _ mouseeffert. margin = new thickness (position. x, position. y, 0, 0 );

82

83 point currentposition = e. getposition (_ board );

84 if (currentposition. x> 0 & currentposition. x <_ board. actualwidth

85 & currentposition. y> 0 & currentposition. y <_ board. actualheight)

86 {

87 eveargs. accept = true;

88}

89 else

90 {

91 eveargs. accept = false;

92}

93}

94}

95}
Show sourceview sourceprint? 01 using system;

02 using system. windows;

03

04 namespace dragdroplibrary

05 {

06 public class dragdropeventargs: eventargs

07 {

08 public bool accept {get; set ;}

09

10 public point position {get; internal set ;}

11

12 public frameworkelement target {get; private set ;}

13

14 public dragdropeventargs (frameworkelement target)

15 {

16 target = target;

17 position = new point (0, 0 );

18}

19}

20}

 

1 2

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.