Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF ViewModel RelayCommand inheritance
    primarykey
    data
    text
    <p>I want to create a special class of Command (I've called it DropDownRelayCommand, which are to be used on a dropdown button which has a content which consists of buttons with command.</p> <p>The idea is that the dropdown button should be disabled (or possibly collapsed) if no buttons in it's content are enabled. I want to create a general solution.</p> <p>I have been unable to do this in XAML (in a general way, anyway).</p> <p>I want this DropDownRelayCommand to have it's own CanExecute methods, which should be examiing all CanExecute of its containing commands. The problem is this error (on the marked argument in the code below) : An object reference is required for the non-static field, method, or property 'Js.Mvvm.DropDownRelayCommand.CanDropDownExecute(T)'. I cannot use "this" because that is not allowed in a constructor.</p> <p>Here's my code (I am aware that the containing commands will propably be recieving canexecute request, making this a bit ineffective. But typically I would have only a handful commands so performance is propably not so important:</p> <pre><code>public class DropDownRelayCommand&lt;T&gt; : RelayCommand&lt;T&gt; { private readonly List&lt;RelayCommand&lt;T&gt;&gt; _commands = new List&lt;RelayCommand&lt;T&gt;&gt;(); private bool _hasAnyCanExecute; #region Constructors public DropDownRelayCommand(Action&lt;T&gt; execute, params RelayCommand&lt;T&gt;[] commands) : base(execute, **CanDropDownExecute**) { AddCommandRange(commands); } public bool CanDropDownExecute(T argument) { _hasAnyCanExecute = false; foreach (RelayCommand&lt;T&gt; command in _commands) { _hasAnyCanExecute = _hasAnyCanExecute || command.CanExecute(argument); } return _hasAnyCanExecute; } #endregion #region Public Methods public void AddCommandRange(params RelayCommand&lt;T&gt;[] commands) { if (commands == null) return; foreach (RelayCommand&lt;T&gt; command in commands) { _commands.Add(command); } } public void RemoveCommandRange(params RelayCommand&lt;T&gt;[] commands) { if (commands == null) return; foreach (RelayCommand&lt;T&gt; command in commands) { if (_commands.Contains(command)) { _commands.Remove(command); } } } #endregion } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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