CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: C#

DelegateCommand in WinRT

1585 Views   
public class DelegateCommand : System.Windows.Input.ICommand
{
    private readonly Action m_Execute;
    private readonly Func<bool> m_CanExecute;
    public event EventHandler CanExecuteChanged;
 
    public DelegateCommand(Action execute)
        : this(execute, () => true) { /* empty */ }
 
    public DelegateCommand(Action execute, Func<bool> canexecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        m_Execute = execute;
        m_CanExecute = canexecute;
    }
 
    public bool CanExecute(object p)
    {
        return m_CanExecute == null ? true : m_CanExecute();
    }
 
    public void Execute(object p)
    {
        if (CanExecute(null))
            m_Execute();
    }
 
    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
            CanExecuteChanged(this, EventArgs.Empty);
    }
}
by Jerry Nixon
  August 20, 2012 @ 10:11pm
Tags:
Description:
public class MyViewModel
{
public DelegateCommand ClickCommand { get; set; }
public MyViewModel() { ClickCommand = new DelegateCommand(ClickCommandExecute, ClickCommandCanExecute); }
public void ClickCommandExecute() { /* execute */ }
public bool ClickCommandCanExecute() { return true; }
}

<Page.DataContext>
<local:MyViewModel />
</Page.DataContext>
<Button Command="{Binding ClickCommand}" Content="Click" />

by nitin.pasumarthy    March 28, 2013 @ 9:09pm

Awesome way to keep the code behind of XAML clean. Thanks.

Add a comment


Report Abuse
brought to you by:
West Wind Techologies