简体   繁体   English

基于 ObservableCollection 中项目的属性使用 CanExecute 创建命令

[英]Create Command with CanExecute based on the Property of an item in an ObservableCollection

I have a ViewModel with a property of type ObservableCollection<T> called Items where T is a class that contains a property called IsSelected of type bool that raises the PropertyChanged event when it's value is changed.我有一个 ViewModel,其类型为ObservableCollection<T>的属性称为Items ,其中T是一个类,其中包含一个名为IsSelected的类型为boolPropertyChanged ,该PropertyChanged在其值更改时引发PropertyChanged事件。

I have a Button that is bound to a Command and I want that Button to be enabled if at least one of the Items' IsSelected property is true and disabled otherwise.我有一个Button绑定到一个Command ,我想那个Button启用如果至少一个Items' IsSelected属性为true ,并禁用否则。 I'm trying to do this with ReactiveCommand from ReactiveUI so it would look something like this:我正在尝试使用ReactiveCommand的 ReactiveCommand 来执行此操作,因此它看起来像这样:

this.SubmitCommand = ReactiveCommand.CreateFromTask(SubmitItems,
     this.WhenAnyValue(x => x.Items).Select(x => x.Any(i => i.IsSelected));

But this doesn't seem to work and I figure that's because the Subscription isn't monitoring the IsSelected property of the Item so when an Item is selected, there's no notification to the Subscription . But this doesn't seem to work and I figure that's because the Subscription isn't monitoring the IsSelected property of the Item so when an Item is selected, there's no notification to the Subscription . Anyway, any help would be greatly appreciate it.无论如何,任何帮助将不胜感激。 I think this ReactiveUI stuff is really cool and I'm trying to learn it.我认为这个 ReactiveUI 的东西真的很酷,我正在努力学习它。

I would use DynamicData.我会使用动态数据。

var canExecute = Items.ToObservableChangeSet()
  .AutoRefreshOnObservable(
    x => x.WhenAnyValue(item => item.IsSelected))
  .ToCollection()
  .Select(x => x.Any(item => item.IsSelected));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF从observablecollection中的选定项目绑定命令属性 - WPF Bind command property from selected item in observablecollection 属性更改时,不会调用按钮命令CanExecute - Button Command CanExecute not called when property changed 依赖属性命令的 CanExecute() 未更新 - Dependency Property command's CanExecute() not updating Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。 我如何继续“观察” canExecute? - DelegateCommand in Prism whose canExecuteMethod is determined by a property in an ObservableCollection. How do I continue to “Observe” canExecute? 根据是否选择了 Xamarin CollectionView 项来确定 ReactiveUI 中的 CanExecute - Determining CanExecute in ReactiveUI based upon whether a Xamarin CollectionView item is selected 编辑项目的属性后对ObservableCollection进行排序 - Sorting an ObservableCollection after editing a property of an item "ListView 未更新 itemsource ObservableCollection 项目属性更改" - ListView not updating on the itemsource ObservableCollection Item Property change .NET MAUI - 如何绑定 ObservableCollection 项的属性 - .NET MAUI - How to bind on a property of an ObservableCollection Item 订阅ObservableCollection项属性已更改 - WPF - Subscribe to ObservableCollection item property changed - WPF 根据项目类别中的日期值对ObservableCollection进行排序 - Sorting ObservableCollection based on date value in the item class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM