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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…