【发布时间】:2015-12-16 12:23:36
【问题描述】:
在我的应用中,我有一个带有两个列表的 ViewModel。还有一个动作是 CanExecute 属性依赖于两个列表状态。我正在使用 ReactiveUI。到目前为止,我得到了以下(工作)实现:
public ReactiveList<Defect> Defects { get; } = new ReactiveList<Defect>();
public ReactiveList<State> States { get; } = new ReactiveList<State>();
public ReactiveCommand<object> Print { get; }
// this stream is artificial it is only needed to get notifications from both above
private IObservable<bool> SelectingStream { get; }
ctor()
{
Defects.ChangeTrackingEnabled = true;
States.ChangeTrackingEnabled = true;
SelectingStream = States.ItemChanged.CombineLatest(Defects.ItemChanged, (a, b) =>
{
// here is the condition that needs to be met in order to can execute action
return States.Count(s => s.IsSelected) == 1 &&
Defects.Any(d => d.IsActive);
});
Print = ReactiveCommand.Create(
this.WhenAnyObservable(x => x.SelectingStream)
);
}
它确实有效,但我认为这种方法更像是解决方法。可以吗?有更直接的解决方案吗?
【问题讨论】:
标签: c# mvvm reactiveui