ReactiveUI TwoWay 绑定一个复选框
ReactiveUI TwoWay binding a checkbox
我有一个带有复选框的简单视图和一个带有 [Reactive]
bool 属性.
的 viewModel
我想更改 UI 上的复选框以传播到 viewModel 属性,但我也希望 UI 复选框状态在viewModel 属性 由外部因素更新。
我假设双向绑定可以做到这一点
this.Bind(ViewModel,
viewModel => viewModel.DepthLabel,
view => view._depthLabel.IsChecked)
.DisposeWith(disposable);
...但是当字段 属性 因外部因素发生变化时 UI 没有响应。
我该怎么做才能让 UI 更新?如果这不是双向绑定的目的,应该在什么时候使用?
What can I do to get the UI to update?
确保 DepthLabel
源 属性 已在调度程序线程上设置。
例如,在下面的示例代码中,需要调用ObserveOnDispatcher()
才能更新视图中的CheckBox
:
public class ViewModel : ReactiveObject
{
[Reactive]
public bool DepthLabel { get; set; }
public ViewModel()
{
Observable.Timer(TimeSpan.FromSeconds(2))
.ObserveOnDispatcher()
.Subscribe(_ => DepthLabel = true);
}
}
我有一个带有复选框的简单视图和一个带有 [Reactive]
bool 属性.
我想更改 UI 上的复选框以传播到 viewModel 属性,但我也希望 UI 复选框状态在viewModel 属性 由外部因素更新。
我假设双向绑定可以做到这一点
this.Bind(ViewModel,
viewModel => viewModel.DepthLabel,
view => view._depthLabel.IsChecked)
.DisposeWith(disposable);
...但是当字段 属性 因外部因素发生变化时 UI 没有响应。
我该怎么做才能让 UI 更新?如果这不是双向绑定的目的,应该在什么时候使用?
What can I do to get the UI to update?
确保 DepthLabel
源 属性 已在调度程序线程上设置。
例如,在下面的示例代码中,需要调用ObserveOnDispatcher()
才能更新视图中的CheckBox
:
public class ViewModel : ReactiveObject
{
[Reactive]
public bool DepthLabel { get; set; }
public ViewModel()
{
Observable.Timer(TimeSpan.FromSeconds(2))
.ObserveOnDispatcher()
.Subscribe(_ => DepthLabel = true);
}
}