[Updated to 2.0] WPF: additional properties to solve drag-and-drop operations in mvvm

Source: Internet
Author: User

Directory

  • Initial version
  • 2.0 update
  • Download

 

Returned directory

Initial version

Drag-and-drop operations in WPF (refer to an article I wrote a long time ago: WPF: idataobject, drag-and-drop operations, and clipboard operations) have some headaches for the mvvm mode, because the data judgment and receipt of drag-and-drop operations are closely related to Interface UI events. Write a simple additional property for mvvm-friendly drag-and-drop operations. You need to set two attributes in XAML: The supported data format (dataformats fields) and the command binding in viewmodel. After that, the viewmodel command is called when the specified control receives the drag-and-drop operation and the data format is correct, the data is also converted to the specified format and passed to the Command Execution in viewmodel as a parameter.

 

Let's look at the sample XAML:

<ListBox itemssource = "{Binding data}">

<LOC: drophelper. dropsupport>

<LOC: dropsupportdata binding = "{binding dropcommand}" dataformat = "{X: static dataformats. filedrop}"/>

</LOC: drophelper. dropsupport>

</ListBox>

 

We want to drag and drop files in ListBox. Then, use this additional property (the dropsupportdata attribute of the drophelper class) to define a new object: dropsupportdata. Set the two attributes to OK. One is the dropcommand command that sets the binding attribute to viewmodel. The other is the dataformat attribute. Since it is a file drag-and-drop operation, it is of course the dataformats. filedrop value.

Finally, the itemssource of ListBox is bound to the data attribute of viewmodel to display data.

 

Let's take a look at the dropcommand execution in viewmodel:

Note: Some mvvm framework icommand execution does not provide calls with parameters, such as mvvm light. You may need to modify the original icommand execution. Of course, there are many icommand executions that support command parameters, such as this.

// Data bound to ListBox on the Interface

Private string [] _ DATA = NULL;

Public String [] Data

{

Get {return _ data ;}

Set {raisepropertychangedex (ref _ data, value );}

}

 

# Region dropcommand

 

Private relaycommand <Object> _ dropcommand;

Public relaycommand <Object> dropcommand

{

Get

{

Return _ dropcommand ?? (_ Dropcommand = new relaycommand <Object> (dodrop, candrop ));

}

}

 

Protected virtual void dodrop (Object Arg)

{

Data = (string []) ARG;

}

 

Protected virtual bool candrop (Object Arg)

{

Return true;

}

 

It is also very simple. The most important thing is the dodrop method, that is, the execution method of the dropcommand command. In this case, the ARG parameter is the converted data (idataobject. getdata method result). Since it is a file drag and drop, it is a string array type. After forced conversion, it is assigned to the data attribute of viewmodel. Because the ListBox interface is bound to the data attribute, the drag-and-drop Data is displayed on the interface:

 

Returned directory

2.0 update

Use the previewdragover event and set routedeventargs. handled is true, which solves the problem that textbox cannot be used in the original version (because the textbox of WPF comes with the drag-and-drop function of text, of course, after setting textbox with this, the drag-and-drop function of textbox's original text will also disappear ).

 

At the same time, the binding attribute of dropsupportdata is changed to bindingpath, which can solve the problem of error after binding is used multiple times.

<LOC: drophelper. dropsupport>

<LOC: dropsupportdata bindingpath = "dropcommand" dataformat = "{X: static dataformats. filedrop}"/>

</LOC: drophelper. dropsupport>

 

 

Returned directory

Download

Download source code of each version

Download Page

Note: the link is the Microsoft SkyDrive page. When downloading, use a browser to download it directly. Some download tools may not be available for downloading.

Source code environment: Microsoft Visual Studio express 2012 for Windows Desktop

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.