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

Is it possible to bind multiple data to a component? #27

Open wang-er-s opened 5 years ago

wang-er-s commented 5 years ago
in lua mvvm,i can do this,second parameter is a table,third para is a decorator,make a suit style let view show
self:bind("num", {"inventory.current_used", "inventory.capacity"},function(current_used, capacity)
    return ("%d/%d"):format(current_used, capacity)
end) --  "5/100"

in c#,I didn't expect any good solution. do you have any good idea? pls tell me ! thanks

ryantrem commented 5 years ago

ugui-mvvm is largely modeled after XAML bindings. Back in WPF XAML, we had something to achieve this called a MultiBinding, which aggregated multiple regular Bindings, and then specified an IMultiValueConverter. If ugui-mvvm had something like this, then in your example above, the second parameter would map to the multiple aggregated bindings, and the third parameter would map to the multi-converter.

I think this could be a good approach for ugui-mvvm, but would definitely require some work. I think something like this would make sense:

  1. Factor out some of the core logic in the PropertyBinding class.
  2. Create a MultiPropertyBinding class that has a collection of the factored out stuff from PropertyBinding. Imagine in the inspector having a collection of elements that look similar to what we see in PropertyBinding today, where each element has a binding source, a binding target, and optionally a converter. Additionally, it would have a target property just like PropertyBinding.
  3. Introduce an IMultiValueConverter interface, and have it be a required property on the MultiPropertyBinding. This is the thing that would combine multiple property values into a single output value.