c# - ReactiveUI TwoWay 绑定(bind)一个复选框

标签 c# mvvm reactiveui

我有一个带有复选框的简单 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/

相关文章:

c# - Winform根目录路径。怎么又来了!

xaml - Xamarin.Forms 可以在 XAML 中的数据上下文中定义数据

c# - 响应式 UI 将 Observable 合并到 ObservableAsPropertyHelper

c# - 如果结果来得太晚,则限制但丢弃结果

reactiveui - 是否应该在停用时调用 WhenActivated?

c# - 如何确定 DataGridView 在绑定(bind)到 SqlDataAdapter 时是否包含未提交的更改

c# - 自动连接到 Windows 10 上的 MS 无线显示器

c# - 如何从自定义目录动态加载程序集,包括其依赖项?

c# - WPF中的WPF命令和事件有什么区别?

wpf - Autofac:将 LifeTimeScope 与 MVVM 中 ViewModel 的生命周期相关联