WPF drag-and-drop

Source: Internet
Author: User

The simple drag implementation is to implement the source control's MouseDown event, and the target control drop event. Call Dragdrop.dodragdrop () to initiate a drag-and-drop operation, and the Dragdrop.dodragdrop () function accepts three parameters: DragSource, data, and allowedeffects. It is particularly important to note that the DragSource parameter. This parameter identifies the message source for the drag operation, and also determines who sent all the message source events. The parameter data is used to wrap the drag&drop. In general, it is an instance of a DataObject type. Within the instance, the data that is actually manipulated by the drag and drop should be wrapped. Finally, allowedeffects can be used to specify the effect of a drag-and-drop operation. The fragment that invokes the function can be as follows:

Dragdrop.dodragdrop (Mlistbox, DataObject, dragdropeffects.copy);

Example 1: Drag the TextBlock control from GroupBox to the right listbox


Xmal:
<Grid> <listbox x:name="ListBox"drop="Listbox_drop"Horizontalalignment=" Left"height=" the"margin="285,10,0,0"Verticalalignment="Top"Width="159"allowdrop="True"/> <groupbox x:name="GroupBox"Header="GroupBox"mousedown="Textblock_mousedown"Horizontalalignment=" Left"margin="23,10,0,0"Verticalalignment="Top"height=" the"Width="247"> <stackpanel margin="Ten"> <textblock margin="Ten"> Football </TextBlock> <textblock margin="Ten"> Basketball </TextBlock> <textblock margin="Ten"> Badminton </TextBlock> <textblock margin="Ten"> Table Tennis </TextBlock> </StackPanel> </GroupBox> </Grid>

C#

  Private void Textblock_mousedown (object  sender, MouseButtonEventArgs e)        {            = (TextBlock) E. originalsource;            Dragdrop.dodragdrop (obj, obj. Text, dragdropeffects.copy);        }         Private void Listbox_drop (object  sender, DragEventArgs e)        {            string data = e.Data.GetData (dataformats.text). ToString ();            LISTBOX.ITEMS.ADD (data);        }

The code is not difficult, oneself should be able to understand, here does not explain.

Example 2: Drag a folder from the outside to the color box in the form to display the folder address and all file information under the folder

The code is as follows:

<grid drop="Textbox_drop"> <textblock x:name="TextBlock"text="folder:"Horizontalalignment=" Left"margin="10,35,0,0"textwrapping="Wrap"Verticalalignment="Top"/> <listbox x:name="ListBox"Horizontalalignment=" Left"height="176"margin="10,100,0,0"Verticalalignment="Top"Width="391"/> <textblock x:name="TextBlock1"Horizontalalignment=" Left"margin="10,72,0,0"textwrapping="Wrap"text="file under folder:"Verticalalignment="Top"/> <textblock x:name="TextBlock2"Horizontalalignment=" Left"margin="10,10,0,0"textwrapping="Wrap"text="Please drag the folder into the color box"Verticalalignment="Top"/> <textblock x:name="Txtfloder"Horizontalalignment=" Left"margin="63,31,0,0"textwrapping="Wrap"Verticalalignment="Top"Width="328"Background="#FFCEDA41"height=" the"/> </Grid>
 Private voidTextbox_drop (Objectsender, DragEventArgs e) {            if(E.data.getdatapresent (DataFormats.FileDrop)) {listBox.Items.Clear (); stringFloder = ((System.Array) e.Data.GetData (DataFormats.FileDrop)). GetValue (0).                ToString (); if(Directory.Exists (Floder)) {Txtfloder. Text=Floder.                                        ToString (); foreach(varIteminchDirectory.GetFiles (Floder))                    {LISTBOX.ITEMS.ADD (item); }                }                ElseTxtfloder. Text="Invalid folder"; }        }

Example 3: Drag the garden from left to right



<Grid>
<canvas name= "CANVAS1" horizontalalignment= "left" height= "259" margin= "10,10,0,0" verticalalignment= "Top" Width= " 228 "background=" #FFEBEABC ">
<ellipse name= "ell" previewmousedown= "Ell_previewmousemove" previewmousemove= "Ell_previewmousemove" Fill= "# Ff9595e5 "height=" canvas.left= "stroke=" Black "canvas.top=" "width="
</Canvas>
<canvas name= "Canvas2" drop= "C2_drop" dragover= "Canvas2_dragover" horizontalalignment= "left" height= "259" Margin= "256,10,0,0" verticalalignment= "Top" width= "214" background= "#FFEDE2E2" allowdrop= "True"/>

</Grid>
private void Ell_previewmousemove (object sender, MouseEventArgs e)
{
if (E.leftbutton = = mousebuttonstate.pressed)
{
DataObject obj = new DataObject (typeof (Ellipse), ell);
Dragdrop.dodragdrop (This.ell, obj, dragdropeffects.move);
}
}
private void C2_drop (object sender, DragEventArgs e)
{
if (E.data.getdatapresent (typeof (Ellipse)))
{
Ellipse obj = e.Data.GetData (typeof (Ellipse)) as Ellipse;
CANVAS1. Children.remove (obj);
Canvas2. Children.add (obj);
}
}

private void Canvas2_dragover (object sender, DragEventArgs e)
{
if (!e.data.getdatapresent (typeof (Ellipse)))
{
E.effects = DragDropEffects.None;
E.handled = true;

}
}


Here the event previewmousedown= "Ell_previewmousemove" previewmousemove= "Ell_previewmousemove", you can write only one of them.

Example 4:

WPF drag-and-drop

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.