簡體   English   中英

ReactiveUI TwoWay綁定復選框

[英]ReactiveUI TwoWay binding a checkbox

我有一個帶有復選框的簡單視圖和一個具有[Reactive] bool屬性的viewModel。

我希望對UI上的復選框進行更改以傳播到viewModel屬性,但是當viewModel屬性由外部因素更新時,我也希望UI復選框狀態得到更新。

我認為雙向綁定可以做到這一點

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

...但是當字段屬性因外部因素而變化時,UI沒有響應。

我該怎么做才能更新用戶界面? 如果這不是雙向綁定的意思,那么什么時候應使用它?

我該怎么做才能更新用戶界面?

確保在調度程序線程上設置了DepthLabel source屬性。

例如,在以下示例代碼中,要更新視圖中的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