ReactiveUI: Using CanExecute with a ReactiveCommand

一曲冷凌霜 提交于 2019-12-01 00:20:35

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");
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!