Using Interaction Triggers to bind Events to View Modals

he method of binding Events to View Modals only differs in the Xaml, rest of the Code remains the Same.

Note:

1 ) All the contents in the xaml Page should be places inside the TAG

for Windows Phone And for Silverlight Applications.

2) This also requires a reference to System.Windows.Interactivity dll.

This example consistes of 4 parts :

1 ) ButtonCommandBinding.cs 2 ) ButtonMouseEnterCommandBindingViewModal.cs 3 ) ButtonMouseEnterCommandBinding.xaml.cs 4 ) ButtonMouseEnterCommandBinding.xaml

——————————————————————————–

Code for ButtonMouseEnterCommandBinding.xaml
This is the view portion for the application here button mouse enter event will be captured and binded to the view model.

you will have to include System.Windows.Interactivity to attach mouse enter event


    

        
            
                
                    
                
            
        


        
    

 
--------------------------------------------------------------------------------- 
Code for ButtonMouseEnterCommandBinding.xaml.cs

This is the code behind for the view where data context will be provided for the entire view

public partial class ButtonMouseEnterCommandBinding : UserControl
    {
        public ButtonMouseEnterCommandBinding()
        {
            InitializeComponent();
            //provide data context to the entire page
            this.DataContext = new ButtonMouseEnterCommandBindingViewModal();
        }
    }

  
----------------------------------------------------------------------------

Code for ButtonMouseEnterCommandBindingViewModal.cs

This is the view modal where the event is registered by calling the ButtonCommandBinding class.
Here the method ShowMouseOver() is called when the event occurrs.

public class ButtonMouseEnterCommandBindingViewModal : INotifyPropertyChanged
    {
        //Text property for textbox
        public string Text { get; set; }

        //Command binding property for text box
        public ButtonCommandBinding MouseEnter { get; set; }
        /// 
        /// Constructor
        /// 
        public ButtonMouseEnterCommandBindingViewModal()
        {
            // Register the method to be called on Mouse Enter
            MouseEnter = new ButtonCommandBinding(ShowMouseOver);
            // Set enable of event execution to true
            MouseEnter.IsEnabled = true;
        }


        /// 
        /// Method to be called when property(value) is changed
        /// 
        /// 
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        

        public void ShowMouseOver()
        {
            // insert logic here for execution of mouse over event
            //change value
            Text = "In view modal";
            NotifyPropertyChanged("Text");
        }

        /// 
        /// Property changed Event 
        /// 
        public event PropertyChangedEventHandler PropertyChanged;
    } 
------------------------------------------------------------------------------------------------- 
 
 
Code for ButtonCommandBinding.cs

This class registers the method to be executed by the click command and executes the method when event occurrs.



public class ButtonCommandBinding : ICommand
    {
        //delegate command to register method to be executed
        private readonly Action handler;
        private bool isEnabled;

        /// 
        /// Bind method to be executed to the handler
        /// So that it can direct on event execution
        /// 
        /// 
        public ButtonCommandBinding(Action handler)
        {
            //Specify the method name to the handler
            this.handler = handler;
        }

        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (value != isEnabled)
                {
                    isEnabled = value;
                    if (CanExecuteChanged != null)
                    {
                        CanExecuteChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        /// 
        /// method to specify if the event will execute
        /// 
        /// 
        /// 
        public bool CanExecute(object parameter)
        {
            return IsEnabled;
        }

        public event EventHandler CanExecuteChanged;

        /// 
        /// This method is called when the event occurs which transfers the 
        /// controll to the method that has been registered
        /// 
        /// 
        public void Execute(object parameter)
        {
            //call the method using the handler
            handler();
        }
    }
150 150 Burnignorance | Where Minds Meet And Sparks Fly!