Silverlight Smart Form Implementation code (1/2)

Source: Internet
Author: User
Tags static class silverlight

Silverlight Smart Form Implementation code

Controls drag from the toolbox to the artboard let me think of the Tookit Dragdroptarget control, Toolbox say with a ListBox or TreeView, the artboard do not know how to do, so decided to do their own dragdrop.

On the left is a ListBox, and the red on the right is canvas. Create a class library Dragdroplibrary (this name doesn't know if it sounds good).

Click on the left side of the ListBox, and then move the mouse to the right side of the Red section, release the mouse control is placed in the loosened position, the idea is so simple.

XAML for the listbox: (controls = new String[3] {"TextBox", "button", "TextBlock"}; This is the data for the listbox, test ... )

<listbox x:name= "toolbox" itemssource= "{Binding controls,mode=onetime}" >
<listbox.itemtemplate>
<datatemplate >
<textblock text= "{Binding}" mouseleftbuttondown= "Onlistboxitemmouseleftbuttondown"/>
</datatemplate>
</listbox.itemtemplate>
</listbox>

Item on a TextBlock, we want to register the MouseLeftButtonDown event.

Add a class to the dragdroplibrary:

Show Sourceview sourceprint?01 using System.Windows;

Using System.Windows.Controls;

Using System.windows.input;

Using System.windows.media;

Using System.windows.media.imaging;

06

Modified namespace Dragdroplibrary

08 {

The public static class Dragdropmanage

10 {

One private static panel _root, _board;

//drop

delegate void Drop (Dragdropeventargs target);

private static Drop _ondrag;

private static Dragdropeventargs Eveargs;

16

17//Mouse Drag time following the effect

A private static image _mouseeffert;

19

///<summary>

21///Drag before calling

///</summary>

///<param name= "root" > Root panel (Children contains toolbox and artboards) </param>

///<param name= "board" > Artboard </param>

public static void register (panel root, panel board)

26 {

_root = root;

_board = Board;

If (_root is grid)

30 {

Grid.setcolumnspan (_mouseeffert, (_root as Grid). Columndefinitions.count+1);

Grid.setrowspan (_mouseeffert, _root as Grid). Rowdefinitions.count+1);

33}

34}

35

Static Dragdropmanage ()

37 {

_mouseeffert = new Image ();

39}

40

The public static void BeginDrag (object sender, MouseButtonEventArgs e, drop drop)

42 {

FrameworkElement target = sender as FrameworkElement;

WriteableBitmap bitmap = new WriteableBitmap (target, New TranslateTransform ());

_mouseeffert.source =bitmap;

_mouseeffert.height = Bitmap.pixelheight;

_mouseeffert.width = Bitmap.pixelwidth;

_root.children.add (_mouseeffert);

Point position = E.getposition (_root);

_mouseeffert.margin = new Thickness (position.x, position.y, 0, 0);

51

_mouseeffert.horizontalalignment = HorizontalAlignment.Left;

_mouseeffert.verticalalignment = Verticalalignment.top;

_mouseeffert.capturemouse ();

Eveargs = new Dragdropeventargs (target);

56

_ondrag = drop;

_root.mousemove + = Onrootmousemove;

_root.mouseleftbuttonup + = Onrootmouseleftbuttonup;

60}

61

The private static void clear ()

63 {

_root.mousemove-= Onrootmousemove;

_root.mouseleftbuttonup-= Onrootmouseleftbuttonup;

_root.children.remove (_mouseeffert);

_ondrag = null;

Eveargs = null;

69}

70

The private static void Onrootmouseleftbuttonup (object sender, MouseButtonEventArgs e)

72 {

Eveargs.position = E.getposition (_board);

_ondrag (Eveargs);

Clear ();

76}

77

Onrootmousemove private static void (object sender, MouseEventArgs e)

79 {

The point position = E.getposition (_root);

Bayi _mouseeffert.margin = new thickness (position.x, position.y, 0, 0);

82

The Point currentposition = E.getposition (_board);

if (currentposition.x > 0 && currentposition.x < _board.actualwidth

&& currentposition.y > 0 && currentposition.y < _board.actualheight)

86 {

Eveargs.accept = true;

88}

Or else

90 {

Eveargs.accept = false;

92}

93}

94}

95}
Show Sourceview sourceprint?01 using System;

Using System.Windows;

03

Namespace Dragdroplibrary

05 {

Dragdropeventargs:eventargs public class

07 {

accept bool {get; set;}

09

Ten public point position {get; internal set;}

11

FrameworkElement target {get; private set;}

13

Public Dragdropeventargs (FrameworkElement target)

15 {

target = target;

Position = new Point (0, 0);

18}

19}

20}

Home 1 2 last page

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.