Data Binding in ReactiveUI¶
A binding connects a property on a view model to a property on a view. When one side changes, the binding writes the new value to the other side. ReactiveUI bindings are written with lambda expressions, so a renamed property breaks the build instead of failing at run time.
ReactiveUI.Binding is the binding engine. A source generator reads each binding at build time and writes the code for it, so bindings need no reflection and work with trimming and Native AOT. The Binding section covers:
- Observing property changes with
WhenChanged,WhenAnyValueand the other observation methods. - Bindings with
BindOneWay,BindTwoWay,BindTo,BindCommandandBindInteraction. - Converters and custom converters for values whose types differ.
- Notification mechanisms, views, threading and setup.
Platforms¶
The platform pages below cover the view base classes and activation for each UI framework. Each one builds on WhenActivated and the platform base classes.
Implement IViewFor on the view¶
A view needs an IViewFor<TViewModel> implementation before it can use the view-first binding methods. The way to implement it depends on the platform.
-
iOS: change the base class to one of the Reactive UIKit classes, such as
ReactiveViewController, and implementViewModelwithRaiseAndSetIfChanged. You can also implementINotifyPropertyChangedon the view and make sure theViewModelproperty signals changes. -
Android: change the base class to one of the Reactive Activity or Fragment classes, such as
ReactiveActivity<T>. You can also implementIViewFor<T>on the view and make sure theViewModelproperty signals changes. -
XAML-based platforms: implement
IViewFor<T>by hand and makeViewModelaDependencyProperty. AUserControlcan use theReactiveUserControl<TViewModel>base class instead.