2
votes

I'm starting to work with the ReactiveUI framework on a Silverlight project and need some help working with ReactiveCommands.

In my view model, I have something that looks roughly like this (this is just a simplified example):

public class MyViewModel : ReactiveObject
{
  private int MaxRecords = 5;

  public ReactiveCommand AddNewRecord { get; protected set; }

  private ObservableCollection<string> _myCollection = new ObservableCollection<string>();
  public ObservableCollection<string> MyCollection
  {
    get
    {
      return _myCollection;
    }

    set
    {
      _myCollection = value;
      raiseCollectionChanged("MyCollection");
    }
  }

  MyViewModel()
  {
    var canAddRecords = Observable.Return<bool>(MyCollection.Count < MaxRecords);
    AddNewRecord = new ReactiveCommand(canAddRecords);

    AddNewRecord.Subscribe(x => 
    {
       MyCollection.Add("foo");
    }
  }
}

The canAddRecords function is getting evaluated the first time the ReactiveCommand is created, but it's not getting re-evaluated when items are added to MyCollection. Can anyone show me a good example of how to bind the canExecute property of a ReactiveCommand so that it gets automatically re-evaluated in this situation?

2

2 Answers

5
votes

Actually, there's a better way to do this, change your ObservableCollection to ReactiveCollection (which inherits from ObservableCollection but adds some extra properties):

MyCollection = new ReactiveCollection<string>();

AddNewRecord = new ReactiveCommand(
    MyCollection.CollectionCountChanged.Select(count => count < MaxRecords));

The catch here now is though, you can't overwrite the MyCollection, only repopulate it (i.e. Clear() + Add()). Let me know if that's a dealbreaker, there's a way to get around that too though it's a bit more work.

3
votes

I finally figured this one out. Using ReactiveCommand.Create() worked for my situation.

MyViewModel()
{
  AddNewRecord = ReactiveCommand.Create(x => MyCollection.Count < MaxRecords);

  AddNewRecord.Subscribe(x => 
  {
     MyCollection.Add("foo");
  }
}