[Updated to 2.0] WPF: Binding predefined commands to viewmodel commands in mvvm

Source: Internet
Author: User

Directory

  • Initial version
  • 2.0 update
  • Download

 

Returned directory

Initial version

In non-mvvm scenarios, developers can directly use commandbinding to provide execution methods for pre-defined commands (such as applicationcommands. Open), and then set pre-defined commands in the corresponding icommandsource. However, in mvvm, command execution is usually performed in viewmodel, and viewmodel directly returns an icommand (such as relaycommand) through attributes ), all of this seems to have completely abandoned the pre-defined commands because commandbinding cannot be used. There are some solutions, such as adding commandbinding to viewmodel, and then adding commandbinding to interface elements by appending properties, but this obviously breaks the definition method of the original viewmodel command.

This method also uses additional attributes, but the command definition in viewmodel does not need to be changed. This method will re-direct the execution of predefined commands to the execution of viewmodel commands, of course, it is implemented by adding commandbinding.

For example, define an okcommand in viewmodel:

// + Using galasoft. mvvmlight. Command;

Private relaycommand _ okcommand;

Public relaycommand okcommand

{

Get

{

Return _ okcommand ?? (_ Okcommand = new relaycommand (dook, canok ));

}

}

 

Protected virtual void dook ()

{

System. Windows. MessageBox. Show ("the command runs successfully ");

}

 

Protected virtual bool canok ()

{

Return true;

}

 

On the interface, two buttons, one can bind the viewmodel command directly, and the other can bind applicationcommands. the open redirection command to viewmodel (that is, the command execution in viewmodel is applicationcommands. open execution). The usage is to append the property through the cmdhelper type redirection. The value of the type is the redirection object. Two properties: The Source property is the icommand type (since it is the icommand type, you can directly use the predefined command). The target attribute is a string that represents the command attribute name in viewmodel.

The sample code is as follows:

<! -- Elements appended with property applications will be added to commandbinding -->

<! -- At this time, the additional property is set to the root window element -->

<LOC: cmdhelper. Redirection>

<! -- Source is a predefined command -->

<! -- Target is the property name of the command in viewmodel -->

<LOC: cmdhelper source = "open" target = "okcommand"/>

</LOC: cmdhelper. Redirection>

<Stackpanel>

<Button command = "open" content = "bind applicationcommands. Open (Try Ctrl + O)"/>

<Button command = "{binding okcommand}" content = "directly bind the command in viewmodel"/>

</Stackpanel>

 

After running, the command will be executed after you click any button or press Ctrl + O.

 

To achieve this, you first obtain the predefined commands and viewmodel commands, and then add a commandbinding. The exected and canexecute events of commandbinding execute the viewmodel command (that is, the icommand interface) and canexecute methods. Finally, add the commandbinding to the commandbindings attribute of the window object of the frameworkelement corresponding to the additional property.

 

 

In the implementation process, the technical problem is that the object with the attached attribute does not inherit the datacontext of the root node, so the target attribute cannot be bound, so only strings can be used, in terms of implementation behind the scenes, the target frameworkelement with an additional attribute is used to obtain datacontext, and then the attribute value is returned through reflection, that is, the command attribute of viewmodel. Therefore, the target attribute type of cmdhelper is a string rather than an icommand.

 

The following code uses the window. getwindow method to obtain the window object where the element is located, and then adds commandbinding to the window. commandbindings attribute.

// The FF variable is the element corresponding to the additional property.

Window. getwindow (ff). commandbindings. Add (binding );

 

Returned directory

2.0 update

If redirection is applied to icommandsource after 2.0, the icommandsource. Command attribute is automatically used as a command without setting the target attribute.

The following code:

<! -- After update 2.0, if redirection is applied to icommandsource, The icommandsource. Command attribute is automatically used as a command without setting the target attribute. -->

<Button command = "{binding okcommand}" content = "bind applicationcommands. Open (Try Ctrl + O)">

<LOC: cmdhelper. Redirection>

<LOC: cmdhelper source = "open"/>

</LOC: cmdhelper. Redirection>

</Button>

 

Of course, another additional property: sourceredirection is added for this purpose, which is more convenient to use:

<! -- You can also directly use cmdhelper. sourceredirection to add Attributes -->

<Button command = "{binding okcommand}" content = "bind applicationcommands. Open (Try Ctrl + O)" LOC: cmdhelper. sourceredirection = "open"/>

 

Both functions are equivalent.

The internal command acquisition method is also updated. In the initial version, reflection is used directly. After 2.0, commands in viewmodel are obtained by binding. Therefore, more complex paths are supported.

 

Returned directory

Download

Download source code of each version

Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.

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

Note: The Source Code does not contain the referenced external class library file: mvvm light

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.