• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTkz

Related Code
Strongly typed NotifyPropertyChanged
Fork of Strongly typed NotifyPropertyChanged
Default command implementation for Silverlight

Silverlight RelayCommand to simplify using the ICommand interface

This code is an example of a ICommand helper, to make it easier to use Commanding in Silverlight

3
1.84k 1 1 0 1 2

public class RelayCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;


    public RelayCommand(
                        Action<object> execute,
                        Predicate<object> canExecute = null)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }


    public void UpdateCanExecuteCommand()
    {
        if (CanExecuteChanged != null)
            CanExecuteChanged(this, new EventArgs());
    }


    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }


    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}


Can be used like this in a ViewModel:

private ICommand _command;

public ICommand MyCommand
{
     get
     {
        if (_command == null)
               _command = new RelayCommand(p =>
                                        {
                                           //DoSomething()
                                        };
         return _command;
     }
}


In XAML:

 <button Command="{Binding MyCommand}" .../>

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

icommand

Mark 'icommand' tag as 'like'

Mark 'icommand' tag as 'ignore'

silverlight

Mark 'silverlight' tag as 'like'

Mark 'silverlight' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 14, 2010 8:01 AM
edited July 15, 2010 11:18 PM

Fork

 Silverlight RelayCommand to simplify using the ICommand interface -  @fredrikn Wednesday 14, 2010 8:01 AM


2 Feedback

Like it -  @luisabreu Wednesday 14, 2010 4:20 PM
Even Josh Smith admits this implementation isn't production ready. -  @TheColonial Saturday 17, 2010 2:08 PM

You must log in before you can give any feedback


2 Discussion(s)

Newest Oldest
0

But I simply hate that initialization code. It looks so 2005 :)

link | flag  | Reply

 @luisabreu
248
Wednesday 14, 2010 4:20 PM

0

@OJ:

Why isn't it production ready? I have used it in several project without any problems at all so for me is production ready, it's a simple helper method only.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Sunday 18, 2010 10:29 AM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010