Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you explain the difference between Source and Target in MvvmCross bindings


Can you explain the difference between Source and Target in MvvmCross bindings


In MvvmCross, when creating bindings, it's important to understand the distinction between Source and Target[1].

* Target: The Target is the property on your View that you are binding[1]. It refers to any public property on a View that you want to bind, such as `Text`, `ItemsSource`, or `SelectedItem`[1]. In MvvmCross, any public property on a View can be bound in `OneWay` mode[1].
* Source: The Source is the property in your ViewModel that your View binds to[1]. The Source in your ViewModel also needs to be a public property[1]. If you implement `INotifyPropertyChanged` on your ViewModel and raise the `NotifyPropertyChanged` event when updating your properties, the MvvmCross binding engine will know how to feed the value to the Target[1][5].

MvvmCross uses `TargetBinding` to define how a View can bind when modes other than `OneWay` are desired[1]. If you want to bind something in `TwoWay` mode, a `TargetBinding` must exist to describe how to achieve this[1]. Internally, a `TargetBinding` typically subscribes to `EventHandler`s to relevant events in order to notify when something has changed on the View, so that the changes can be fed back to the ViewModel[1]. You can create your own `TargetBinding`[1][4].

Binding Expressions

* Swiss/Tibet: Android bindings and some string binding descriptions use Swiss/Tibet binding expressions, a description language specific to MvvmCross[1].


    
    

In this case, `SomeView` is the bindable object (the View), `ViewProperty` is the Target, and `ViewModelProperty` is the Source[1].
* XAML: In XAML binding expressions, bindings typically look like this:

    
    

`SomeView` is the bindable object, `ViewProperty` is the Target, and `ViewModelProperty` is the Source[1]. With MvvmCross XAML BindingEx extensions, which use Swiss/Tibet, you can alternatively bind like this:

    
    

* Fluent Bindings: MvvmCross also offers Fluent Bindings to create type-safe binding expressions in code behind[1]. For these to work, the place where you create the fluent binding must inherit from `IMvxBindingContextOwner`, meaning the type will have a `BindingContext` property that the Fluent Binding will use to find the Source[1][3]. These Fluent Bindings usually look like this:

    var set = this.CreateBindingSet();
    set.Bind(someView)
    .For(view => view.ViewProperty)
    .To(viewModel => viewModel.ViewModelProperty);
    

The `ContextOwnerType` is usually a `MvxViewController`, `MvxTableViewCell`, or `MvxActivity`. This is typically the type containing the views you want to bind to[1]. When using the `BindingSet.Bind()` method, it expects the Target as an argument, typically the actual View you want to bind to[1].

Citations:
[1] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[2] https://stackoverflow.com/questions/30146384/how-to-bind-mvxtableviewsource-to-dynamically-created-viewmodel-with-mvvmcross-a/30229955
[3] https://stackoverflow.com/questions/30146384/how-to-bind-mvxtableviewsource-to-dynamically-created-viewmodel-with-mvvmcross-a
[4] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[5] https://www.packtpub.com/en-us/learning/how-to-tutorials/sharing-mvvmcross
[6] https://canbilgin.wordpress.com/tag/mvvmcross/
[7] https://www.mvvmcross.com/documentation/fundamentals/value-converters
[8] https://github.com/MvvmCross/MvvmCross/issues/87