Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does MvvmCross handle TwoWay binding in AXML compared to OneWay binding in XAML


How does MvvmCross handle TwoWay binding in AXML compared to OneWay binding in XAML


MvvmCross is a framework that facilitates the Model-View-ViewModel (MVVM) pattern across multiple platforms, including Android and Windows. It supports various binding modes, such as OneWay, TwoWay, and OneTime. Here's how MvvmCross handles TwoWay binding in AXML compared to OneWay binding in XAML:

TwoWay Binding in AXML

In MvvmCross, TwoWay binding is commonly used when you need to update both the View and the ViewModel. This mode is particularly useful for editing forms where changes in the View should reflect in the ViewModel and vice versa. When using AXML (Android XML), MvvmCross typically defaults to TwoWay binding for custom bindings, but native properties usually require a custom binding to achieve this behavior.

To implement TwoWay binding in AXML, you would use the `MvxBind` attribute, specifying the property of the control and the property of the ViewModel. For example:

xml

In this case, if you want explicit TwoWay binding, you might need to specify it using the `Mode=TwoWay` syntax, although it's often the default for custom bindings:

xml

However, for native properties without built-in support for TwoWay binding, you would need to create a custom binding.

OneWay Binding in XAML

In XAML, particularly for Windows and Xamarin.Forms, the default binding mode is typically OneWay. This means that data is transferred from the ViewModel to the View, but changes in the View do not automatically update the ViewModel. To achieve OneWay binding in XAML, you simply use the `Binding` markup extension without specifying a mode:

xml

If you need TwoWay binding in XAML, you must explicitly specify it by adding the `Mode=TwoWay` attribute to the binding expression:

xml

This ensures that changes in the TextBox are reflected in the ViewModel and vice versa.

Key Differences

- Default Binding Mode: In MvvmCross, TwoWay is often the default for custom bindings in AXML, while in XAML, OneWay is typically the default.
- Implementation: AXML uses the `MvxBind` attribute for bindings, whereas XAML uses the `Binding` markup extension.
- Custom Bindings: For native properties without built-in TwoWay support, custom bindings are required in both AXML and XAML to enable TwoWay binding.

Overall, MvvmCross provides flexible data binding options across different platforms, allowing developers to choose the most appropriate binding mode based on their application's requirements.

Citations:
[1] https://www.mvvmcross.com/documentation/fundamentals/data-binding
[2] https://stackoverflow.com/questions/41768962/mvvmcross-default-binding-mode-used-in-a-project
[3] http://stackoverflow.com/questions/10700445/in-mvvmcross-how-do-i-do-custom.-bind-properties
[4] https://stackoverflow.com/questions/17156368/
[5] https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm?view=net-maui-9.0
[6] https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-9.0
[7] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[8] https://www.mvvmcross.com/documentation/tutorials/tipcalc/a-wpf-ui-project
[9] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[10] https://www.mvvmcross.com/documentation/advanced/custom-data-binding