简体   繁体   English

ReactiveUI TwoWay绑定复选框

[英]ReactiveUI TwoWay binding a checkbox

I have a simple view with a checkbox and a viewModel with a [Reactive] bool property. 我有一个带有复选框的简单视图和一个具有[Reactive] bool属性的viewModel。

I would like a change to the checkbox on the UI to propagate to the viewModel property, but I would also like to have the UI checkbox state get updated when the viewModel property is updated by external factors. 我希望对UI上的复选框进行更改以传播到viewModel属性,但是当viewModel属性由外部因素更新时,我也希望UI复选框状态得到更新。

I assumed that a two way binding would do this 我认为双向绑定可以做到这一点

this.Bind(ViewModel, viewModel => viewModel.DepthLabel, view => view._depthLabel.IsChecked) .DisposeWith(disposable);

...but the UI is not responding when the field property changes from external factors. ...但是当字段属性因外部因素而变化时,UI没有响应。

What can I do to get the UI to update? 我该怎么做才能更新用户界面? If this is not what two-way bind is meant for, when should it be used? 如果这不是双向绑定的意思,那么什么时候应使用它?

What can I do to get the UI to update? 我该怎么做才能更新用户界面?

Make sure that the DepthLabel source property is set on the dispatcher thread. 确保在调度程序线程上设置了DepthLabel source属性。

For example, in the following sample code, calling ObserveOnDispatcher() is required for the CheckBox in the view to update: 例如,在以下示例代码中,要更新视图中的CheckBox ,需要调用ObserveOnDispatcher()

public class ViewModel : ReactiveObject
{
    [Reactive]
    public bool DepthLabel { get; set; }

    public ViewModel()
    {
        Observable.Timer(TimeSpan.FromSeconds(2))
           .ObserveOnDispatcher()
           .Subscribe(_ => DepthLabel = true);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM