Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I solved this problem by creating a DelegateCommand class. It looks exactly like the RelayCommand (See Josh Smith), with the exception that it allows for updating the callbacks.</p> <pre><code>public class DelegateCommand : ICommand { Action&lt;object&gt; _execute; Predicate&lt;object&gt; _canExecute; #region Constructors public DelegateCommand() { } public DelegateCommand(Action&lt;object&gt; execute) : this(execute, null) { } public DelegateCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) throw new ArgumentNullException("execute"); _execute = execute; _canExecute = canExecute; } #endregion // Constructors public void Delegate(Action&lt;object&gt; execute) { _execute = execute; } public void Delegate(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { _execute = execute; _canExecute = canExecute; } #region ICommand Members [DebuggerStepThrough] public bool CanExecute(object parameter) { return _canExecute == null ? _execute != null : _canExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { if (CanExecute(parameter)) _execute(parameter); } #endregion // ICommand Members } </code></pre> <p>Then I created a class to hold static application commands.</p> <pre><code>public class CustomCommands { private readonly static DelegateCommand admin; static CustomCommands() { admin = new DelegateCommand(); } public static DelegateCommand AdminCommand { get { return admin; } } } </code></pre> <p>Then I added a key binding to the main window since user controls don't receive key gestures.</p> <pre><code>&lt;Window.InputBindings&gt; &lt;KeyBinding Key="A" Modifiers="Control" Command="my:CustomCommands.AdminCommand"/&gt; &lt;/Window.InputBindings&gt; </code></pre> <p>Then, in my ViewModel I can handle the event like this:</p> <pre><code>public class OfflineViewModel : ViewModelBase { public OfflineViewModel() { CustomCommands.AdminCommand.Delegate(ShowAdmin); } public override void Removed() { base.Removed(); ReleaseAdminCommand(); } public override void Hidden() { base.Hidden(); ReleaseAdminCommand(); } void HookAdminCommand() { CustomCommands.AdminCommand.Delegate(ShowAdmin); } void ReleaseAdminCommand() { // Remove handling CustomCommands.AdminCommand.Delegate(null, null); } void ShowAdmin(object parameter) { Navigation.Push(new AdminViewModel()); } } </code></pre> <p>Optionally I could use events inside the DelegateCommand.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload