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?
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.
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");
}
}
来源:https://stackoverflow.com/questions/6754928/reactiveui-using-canexecute-with-a-reactivecommand