jbruening / ugui-mvvm

Unity3D uGUI mvvm databinding via the standard IXChanged interfaces used in wpf (INotifyPropertyChanged, INotifyCollectionChanged, etc)
Other
214 stars 41 forks source link

INPCBinding fail if disabled before Start and enabled after DataContext is attached #9

Closed davidwangms closed 5 years ago

davidwangms commented 5 years ago

Repro Steps

  1. Create a hierarchy where an INPCBinding exists on a disabled gameObject
  2. Assign a DataContext
  3. Re-enable the gameObject hierarchy which contains the INPCBinding

Actual INPCBinding will not bind to the ViewModel properly

Expected INPCBinding should still bind to the view model

Investigation INPCBindings register with the DataContext on Start(), and the bindings to the ViewModel are only done when the DataContext is set. This means that disabled INPCBindings will not be registered with the DataContext when ViewModels are set, and so will not be subscribed to the INPC event.

One workaround right now is to have a helper that rebinds the DataContext when one of these object hierarchies is enabled for the first time. However, it would be expected that new bindings that are registered while a DataContext is available should be bound immediately.

ryantrem commented 5 years ago

I think INPCBinding::AddDependentProperty just needs to call AddHandler and TriggerHandler like in UpdateValue.

ryantrem commented 5 years ago

@jbruening - this issue was resolved by @Alex-MSFT's PR (#10).