Skip to content

Commit

Permalink
BindableReactiveProperty supports WinForms binding, fix #225
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Aug 19, 2024
1 parent cf13052 commit 1061981
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
56 changes: 39 additions & 17 deletions sandbox/WinFormsApp1/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
using R3;
using R3.WinForms;
using System.ComponentModel;
using System.Diagnostics;

namespace WinFormsApp1;

public partial class Form1 : Form
{
private readonly BindableReactiveProperty<string> rp = new("");

public Form1()
{
InitializeComponent();

this.components ??= new System.ComponentModel.Container();


// Bind label1.Text to rp.Value
label1.DataBindings.Add("Text", rp, "Value");
}

protected override void OnCreateControl()
{
base.OnCreateControl();

Observable
.EveryValueChanged(
this,
static form => form.Width)
.Subscribe(x =>
{
this.Text = $"Width: {x:#,0}";
})
.AddTo(this.components);

Observable
.FromEventHandler(
handler => this.button1.Click += handler,
handler => this.button1.Click -= handler)
.Delay(TimeSpan.FromSeconds(1))
.Subscribe(_ => this.label1.Text = ObservableSystem.DefaultTimeProvider.GetLocalNow().ToString())
.AddTo(this.components);
button1.Click += button1_Click;

var prop = TypeDescriptor.GetProperties(rp).Find("Value", true);
Debug.WriteLine((
prop!.ComponentType.Name, // "ReactiveProperty`1" (not Bindable)
prop.SupportsChangeEvents // false
));

//Observable
// .EveryValueChanged(
// this,
// static form => form.Width)
// .Subscribe(x =>
// {
// this.Text = $"Width: {x:#,0}";
// })
// .AddTo(this.components);

//Observable
// .FromEventHandler(
// handler => this.button1.Click += handler,
// handler => this.button1.Click -= handler)
// .Delay(TimeSpan.FromSeconds(1))
// .Subscribe(_ => this.label1.Text = ObservableSystem.DefaultTimeProvider.GetLocalNow().ToString())
// .AddTo(this.components);
}

public void button1_Click(object? sender, EventArgs e)
{
// This change won't be notified to label1
rp.Value += "X";
}
}
7 changes: 7 additions & 0 deletions src/R3/BindableReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public BindableReactiveProperty(T value, IEqualityComparer<T>? equalityComparer)
{
}

// WinForms reflection data binding require to impl Value in this type
public new T Value
{
get => base.Value;
set => base.Value = value;
}

// ToBindableReactiveProperty

internal BindableReactiveProperty(Observable<T> source, T initialValue, IEqualityComparer<T>? equalityComparer)
Expand Down

0 comments on commit 1061981

Please sign in to comment.