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?