Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindableReactiveProperty does not notify value changes in the WinForms binding system #225

Closed
eudika opened this issue Jun 20, 2024 · 1 comment

Comments

@eudika
Copy link

eudika commented Jun 20, 2024

Phenomenon

When you bind an instance of R3.BindableReactiveProperty<T> to a WinForms control using its DataBindings property, changes on the Value property of the instance are not notified to the control.
This behavior differs from Reactive.Bindings.ReactiveProperty<T>, which works as expected.

Reproduction

Here's a minimal snippet:

public partial class Form1
{
    // label1 and button1 are defined in the code behind
    
    private readonly BindableReactiveProperty<string> rp = new("");

    public Form1() {
        InitializeComponent();

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

    public void button1_Click()
    {
        // This change won't be notified to label1
        rp.Value += "X";
    }
}

Cause

The problem lies in event registration, which is eventually achieved by the AddValueChanged method of the ReflectPropertyDescriptor instance corresponding to the Value property.
This method checks the following conditions:

  1. Does the component type have an event named <propertyName>Changed? (i.e. ValueChanged)
  2. Does the component type implement INotifyPropertyChanged interface?

The issue arises because the "component type" here is ReactiveProperty<T>, rather than BindableReactiveProperty<T>, since the Value property is defined in the former.
Unlike the Reactive.Bindings version, where the event and the property are defined simultaneously, neither condition 1. nor 2. is met.
As a result, the property is recognized as "not supporting change events."
You can verify this:

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

I hope this clarifies the issue and helps you improve the package.

@neuecc neuecc closed this as completed in 1061981 Aug 19, 2024
@neuecc
Copy link
Member

neuecc commented Aug 19, 2024

Sorry for the delay in replying.
Thank you for the detailed report!
Thanks to you, we were able to recognize the problem and fix it.
We will release it in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants