Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know two ways how to do this.</p> <p><strong>1)</strong> Add new class, for example, <code>MainWindowViewModel</code> and add there 2 commands and an instance of <code>UserControlViewModel</code> (you haven't said the title, so I will call it in this way). Here is a part of example:</p> <pre><code>public class MainWindowViewModel { public UserControlViewModel ChildControlViewModel { get; set; } private Lazy&lt;RelayCommand&gt; nextCommand = new Lazy&lt;RelayCommand&gt;(() =&gt; new RelayCommand( () =&gt; this.ChildControlViewModel.CollectionView.MoveCurrentToNext(), () =&gt; this.ChildControlViewModel.CollectionView.CurrentPosition &lt; this.ChildControlViewModel.ItemTypes.Count - 1)); public ICommand NextCommand { get { return nextCommand.Value; } } //prev command... } </code></pre> <p>I have used the <code>Lazy</code> class, but the main idea is clear: the code is the same, except the call <code>this.ChildControlViewModel.CollectionView</code> instead of <code>CollectionView</code>.</p> <p><strong>2)</strong> Use the <code>Messenger</code> class.</p> <p>This way isn't so obvious and it has only one advantage: the viewmodels are loosely connected.</p> <pre><code>public class MainWindowViewModel { public const string NextCommandNotification = "NextCommand"; public const string PreviousCommandNotification = "PreviousCommand"; private bool isNextCommandEnabled; private bool isPreviousCommandEnabled; public MainWindowViewModel() { this.NextCommand = new RelayCommand( () =&gt; Messenger.Default.Send(new NotificationMessage&lt;MainWindowViewModel&gt;(this, NextCommandNotification)), () =&gt; this.isNextCommandEnabled); //prev command... Messenger.Default.Register&lt;NotificationMessage&lt;UserControlViewModel&gt;&gt;(this, msg =&gt; { if (msg.Notification == UserControlViewModel.CurrentItemChangedNotification) { this.isNextCommandEnabled = msg.Content.CollectionView.CurrentPosition &lt; msg.Content.ItemTypes.Count - 1; this.NextCommand.RaiseCanExecuteChanged(); //prev command... } }); } public ICommand NextCommand { get; private set; } //prev command... } public class UserControlViewModel { public const string CurrentItemChangedNotification = "CurrentItemChanged"; public UserControlViewModel() { Messenger.Default.Register&lt;NotificationMessage&lt;MainWindowViewModel&gt;&gt;(this, msg =&gt; { if (msg.Notification == MainWindowViewModel.NextCommandNotification) this.CollectionView.MoveCurrentToNext(); else if (msg.Notification == MainWindowViewModel.PreviousCommandNotification) this.CollectionView.MoveCurrentToPrevious(); }); this.CollectionView.CurrentChanged += (s,e) =&gt; Messenger.Default.Send(new NotificationMessage&lt;UserControlViewModel&gt;(this, CurrentItemChangedNotification)) } } </code></pre> <p>I'm not sure whether this code will work correctly. And it is not easy to explain.</p> <p>The <code>MainWindowViewModel</code> class send the message when a user press the button. The <code>UserControlViewModel</code> class process the message, change the position of the current item, and send the <code>CurrentItemChangedNotification</code> message. The <code>MainWindowViewModel</code> class process this message and updates the <code>CanExecute</code> part of the command.</p> <p>1st solution is better for me, but at the same time I use the <code>Messenger</code> class quite often. It depends on the situation.</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.
 

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