ReactiveUI: Using CanExecute with a ReactiveCommand

前端 未结 2 1769
挽巷
挽巷 2021-01-07 07:33

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 tha

相关标签:
2条回答
  • 2021-01-07 07:55

    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");
      }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题