MVVM design pattern and implementation in WPF (iv) Event binding

Source: Internet
Author: User

Series Catalog:

MVVM pattern parsing and implementation in WPF (i) Introduction to the MVVM pattern

MVVM pattern parsing and implementation in WPF (ii) data binding

MVVM pattern parsing and implementation in WPF (iii) command binding

MVVM pattern parsing and implementation in WPF (iv) Event binding

MVVM pattern parsing and implementation in WPF (V) View and ViewModel communication

MVVM pattern parsing and implementation in WPF (vi) configuring ViewModel and registering messages with dependency injection

0x00 Why to bind an event

This problem is really well understood, because events are rich and varied, and simple command bindings are far from covering all events. For example, the command binding of a button can resolve the need for the Click event, but what about a large number of events such as the MouseEnter of the button, the loaded of the form, and so on? This will use the event bindings.

0x01 Event Bindings

To use event binding requires the help of system.windows. Interactivity, if you install blend, the DLL is included. You need to add one or more EventTrigger in the Interaction.triggers and specify the event name of interest, and in EventTrigger, bind the command to the event by Invokecommandaction. The diagram shows a loaded event bound to the main window, which invokes the Execute method of the bound command object Loadedcommand after the event is triggered, and can be implemented by binding CommandParameter when the command binding requires parameters. It should be noted that we added the CanExecute judgment before implementing the Execute method of mycommand, so that the command to actually execute the binding after the event is triggered is also affected by the CanExecute method of the bound Loadedcommand.

0x02 event bindings with EventArgs parameters

The event bindings described above are not sufficient to handle all situations, because in many cases we also need to get the data from the EventArgs of the event, such as getting the mouse position and key state from the MouseMove event argument. However, the Invokecommandaction parameter passed to the Execute method without binding to CommandParameter is null. So we need to write a class ourselves to handle the binding of the event to the command.

Look at the invokecommandaction we used above, inherit from Triggeraction<dependencyobject>,triggeraction is an abstract class, We just have to inherit this class and implement the Invoke method. TriggerAction is described in MSDN as follows:

Https://msdn.microsoft.com/zh-cn/library/system.windows.interactivity.triggeraction (v=expression.40). aspx

I simply implemented the following, as shown in the code, where the dependency property is generated with the help of the PROPDP code snippet, or it is hard to remember how to enter so much code. The use of the time to replace the previous invokecommandaction, CommandParameter is not bound to pass is the parameters of the event. If CommandParameter is bound, then the binding parameter is passed.

Example of 0X03 event binding

With Myeventcommand we can bind the event and get the event arguments. The loaded and MouseMove events of the form are bound in the instance, where in the MouseMove event we receive the event object using our own Myeventcommand object and show the position of the mouse relative to the form and the state of each key.

The sample program runs as follows

0x04 Related Downloads

Https://github.com/durow/TestArea/tree/master/MVVMTest/EventBindingTest

MVVM design pattern and implementation in WPF (iv) Event binding

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.