Language: C#
DelegateCommand in WinRT
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); } }
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" />
{
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" />
Report Abuse
Subscribe
Discuss
News
About
New Snippet
Recent Snippets
My Snippets
Favorites
Web Code
Search
Copy
Line#
Awesome way to keep the code behind of XAML clean. Thanks.