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

DelegateCommand in WinRT

2935 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 <br /> { <br /> public DelegateCommand ClickCommand { get; set; } <br /> public MyViewModel() { ClickCommand = new DelegateCommand(ClickCommandExecute, ClickCommandCanExecute); } <br /> public void ClickCommandExecute() { /* execute */ } <br /> public bool ClickCommandCanExecute() { return true; }<br /> }<br /> <br /> <Page.DataContext><br /> <local:MyViewModel /><br /> </Page.DataContext><br /> <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