Skip to content

Instantly share code, notes, and snippets.

@runceel
Created February 8, 2021 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runceel/d860480491c359790414d79148f4338b to your computer and use it in GitHub Desktop.
Save runceel/d860480491c359790414d79148f4338b to your computer and use it in GitHub Desktop.
using Reactive.Bindings;
using Reactive.Bindings.Binding;
using Reactive.Bindings.Extensions;
using System;
using System.Data;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
private CompositeDisposable Disposables { get; } = new CompositeDisposable();
private Form1ViewModel ViewModel { get; }
public Form1()
{
InitializeComponent();
ViewModel = new Form1ViewModel();
FormClosed += (_, __) => Disposables.Dispose();
ViewModel.Name.BindTo(
textBox1,
x => x.Text,
BindingMode.TwoWay,
targetUpdateTrigger: Observable.FromEventPattern(h => textBox1.TextChanged += h, h => textBox1.TextChanged -= h).ToUnit())
.AddTo(Disposables);
ViewModel.UpperCase.BindTo(
label1,
x => x.Text)
.AddTo(Disposables);
}
}
public class Form1ViewModel
{
public ReactiveProperty<string> Name { get; }
public ReadOnlyReactiveProperty<string> UpperCase { get; }
public Form1ViewModel()
{
Name = new ReactiveProperty<string>("");
UpperCase = Name.Select(x => x.ToUpper())
.Delay(TimeSpan.FromSeconds(3))
.ObserveOnUIDispatcher()
.ToReadOnlyReactiveProperty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment