[英]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
的类型为bool
的PropertyChanged
,该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.