This code is an example of a ICommand helper, to make it easier to use Commanding in Silverlight
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}" .../>
Fork
Silverlight RelayCommand to simplify using the ICommand interface - @fredrikn Wednesday 14, 2010 8:01 AM
2 Feedback
Even Josh Smith admits this implementation isn't production ready. - @TheColonial Saturday 17, 2010 2:08 PMYou must log in before you can give any feedback
You must log in before you can post a comment


1.84k
1



Mark 'c#' tag as 'like'
Mark 'c#' tag as 'ignore'