我有一个带有复选框的简单 View 和一个带有 [Reactive]
的 viewModel bool 属性。
我希望更改 UI 上的复选框以传播到 viewModel 属性,但我还希望在 viewModel 属性由外部因素更新时更新 UI 复选框状态。
我认为双向绑定(bind)可以做到这一点this.Bind(ViewModel,
viewModel => viewModel.DepthLabel,
view => view._depthLabel.IsChecked)
.DisposeWith(disposable);
...但是当字段属性因外部因素而变化时,UI 没有响应。
我该怎么做才能让 UI 更新?如果这不是双向绑定(bind)的用途,那么什么时候应该使用它?
最佳答案
What can I do to get the UI to update?
确保
DepthLabel
source 属性在调度程序线程上设置。例如,在下面的示例代码中,调用
ObserveOnDispatcher()
CheckBox
是必需的为了更新:public class ViewModel : ReactiveObject
{
[Reactive]
public bool DepthLabel { get; set; }
public ViewModel()
{
Observable.Timer(TimeSpan.FromSeconds(2))
.ObserveOnDispatcher()
.Subscribe(_ => DepthLabel = true);
}
}
关于c# - ReactiveUI TwoWay 绑定(bind)一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814081/