Those who have developed Windows Phone or Silverlight must know that there is an interactive DLL that can add behavior and eventtrigger to the element. However, the corresponding class libraries are not officially provided in Windows 8, but we can't do it if it's not equal. In fact, we can develop an interactive class library by ourselves. This class library is already included in my open-source class library. Next I will introduce how to use eventtrigger to bind events to elements in the template.
As you know, you cannot directly add events to controls in templates.CodeAfter obtaining the control, add it as code. However, this method is very troublesome. The trouble is that you need to traverse the tree and obtain the control in the tree. It is not convenient to use the XAML code in the template to register events. Eventtrigger is dedicated to this purpose. You only need to specify the event name for eventtrigger, and then specify the callback (icommand) when the event is triggered. The implementation code is as follows:
< Controltemplate X: Key = "Contentcontroltemplate" Targettype = "Contentcontrol" > < Grid > < Button Content = "Click me" > < I: interaction. triggers > < I: eventtrigger Eventname = "Click" Command =" {Binding clickcommand, source = {staticresource mainpageviewmodel }} " /> </ I: interaction. triggers > </ Button > </ Grid > </ Controltemplate >
Eventname is the name of the specified event. I have specified click here. You can replace it with any event name you want.
The interactive usage here is no different from that in Windows Phone and Silverlight.
You can use behavior to add a behavior for a widget. Google can be used if you are not familiar with it.
Source code download, including the Library source code