.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Invoke Commands in Silverlight

Posted By:Mahadesh Mahalingappa       Posted Date: August 12, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

This article would help us understand how we can create simple commands and bind it to our Silverlight Controls
 

This article would help us understand how we can create simple commands and bind it to our Silverlight Controls .

Created a Simple Project as SLSimpleCommands .

Add a Child Window Control as shown below :

child window in silverlight

Lets create the Command now . Here I am creating a simple command which would open our child Window.

Our Command should implement the ICommand Interface .

namespace System.Windows.Input
{
    // Summary:
    //     Defines the contract for commanding, using the same contract as used in WPF.
    public interface ICommand
    {
        // Summary:
        //     Occurs when changes occur that affect whether the command should execute.
        event EventHandler CanExecuteChanged;

        // Summary:
        //     Defines the method that determines whether the command can execute in its
        //     current state.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to null.
        //
        // Returns:
        //     true if this command can be executed; otherwise, false.
        bool CanExecute(object parameter);
        //
        // Summary:
        //     Defines the method to be called when the command is invoked.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to null.
        void Execute(object parameter);
    }
}
Lets create a new ViewModel Class as shown below :
   
 
public class ViewModel
{

    public ICommand PopupVM
    {
        get
        {
            return new InvokeChildCommand();
        }
    }

    public class InvokeChildCommand : ICommand
    {

        public bool CanExecute(object parameter)
        {
            if (parameter != null)
            {
                CanExecuteChanged.Invoke(parameter, new EventArgs());
            }
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //Push the ViewModel into the Popup
            Child child = new Child();
            child.Show();
        }
    }
}
Lets modify the xaml code of MainPage.xaml as shown below :

 
  
  


Lets Give it a run :


silverlight child window

Hit the Button. The child window would be Opened.

This was a simple example where I created a simple command and binded it to the Silverlight Control.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend