The TargetBinding mechanism in MvvmCross is a crucial component of its data binding system, allowing developers to define how views can be bound to view models. This mechanism is essential for creating custom bindings, especially when dealing with views that do not support two-way binding out of the box or require additional parameters.
Key Components of TargetBinding
1. Target and Source:
- Target: This refers to the property on the view that you want to bind. It can be any public property of the view, such as `Text`, `ItemsSource`, or `SelectedItem`.
- Source: This is the property in the view model that the view binds to. It must also be a public property.
2. Binding Modes:
- MvvmCross supports various binding modes, including OneWay, TwoWay, and OneWayToSource. The TwoWay mode is commonly used as it updates both the view and the view model when either changes.
3. TargetBinding Classes:
- MvvmCross provides several base classes for creating custom target bindings:
- MvxTargetBinding: The base class for all target bindings, implementing `IMvxTargetBinding`. It provides methods like `SetValue()` and `SubscribeToEvents()`.
- MvxConvertingTargetBinding: A subclass of `MvxTargetBinding`, it supports using converters and combiners, preventing feedback loops.
- MvxPropertyInfoTargetBinding: A subclass of `MvxConvertingTargetBinding`, it simplifies creating one-way bindings by automatically implementing `SetValue()` based on `PropertyInfo`.
- MvxWithEventPropertyInfoTargetBinding: Similar to `MvxPropertyInfoTargetBinding`, but designed for two-way bindings based on a specific event.
- MvxEventNameTargetBinding: Used for one-way bindings to commands based on specific events.
Creating a Custom TargetBinding
To create a custom target binding, you typically need to:
1. Define the Target Binding Class:
- Create a class that inherits from one of the base classes provided by MvvmCross, such as `MvxPropertyInfoTargetBinding`.
- Override necessary methods like `SetValueImpl()` to define how to set the target property.
- If using two-way binding, override `SubscribeToEvents()` to listen for changes on the view.
2. Register the Target Binding:
- In your `Setup.cs` file, override `FillTargetFactories()` to register your custom target binding.
- Use `RegisterPropertyInfoBindingFactory()` to link your binding class with the specific view and property it targets.
Example of a Custom TargetBinding
Here's an example of creating a custom target binding for a view's property:
csharp
public class MyViewMyPropertyTargetBinding : MvxPropertyInfoTargetBinding
{
public override MvxBindingMode DefaultMode => MvxBindingMode.TwoWay;
public MyViewMyPropertyTargetBinding(object target, PropertyInfo targetPropertyInfo)
: base(target, targetPropertyInfo)
{
}
protected override void SetValueImpl(object target, object value)
{
var view = target as MyView;
if (view == null) return;
view.MyProperty = (string)value;
}
public override void SubscribeToEvents()
{
var myView = View;
if (myView == null) return;
myView.MyPropertyChanged += HandleMyPropertyChanged;
}
private void HandleMyPropertyChanged(object sender, EventArgs e)
{
var myView = View;
if (myView == null) return;
FireValueChanged(myView.MyProperty);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (isDisposing)
{
var myView = View;
if (myView != null)
{
myView.MyPropertyChanged -= HandleMyPropertyChanged;
}
}
}
}
Then, register it in `Setup.cs`:
csharp
protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
base.FillTargetFactories(registry);
registry.RegisterPropertyInfoBindingFactory(
typeof(MyViewMyPropertyTargetBinding),
typeof(MyView), "MyProperty");
}
Conclusion
The TargetBinding mechanism in MvvmCross provides a flexible way to customize data bindings for views, allowing developers to support two-way bindings and handle specific view requirements. By creating custom target bindings, developers can extend the capabilities of MvvmCross to fit their application's needs.
Citations:[1] https://www.mvvmcross.com/documentation/advanced/custom-data-binding
[2] https://www.packtpub.com/en-us/learning/how-to-tutorials/sharing-mvvmcross
[3] https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/august/windows-phone-build-mvvm-apps-with-xamarin-and-mvvmcross
[4] https://blog.ostebaronen.dk/2018/01/mvvmcross-binding-target.html
[5] https://github.com/MvvmCross/MvvmCross-Samples
[6] https://github.com/MvvmCross/MvvmCross/blob/develop/CHANGELOG.md
[7] https://leaware.com/creating-cross-platform-xamarin-applications-using-the-mvvmcross-framework/
[8] https://www.mvvmcross.com/mvvmcross-6.4.0-release/
[9] https://www.mvvmcross.com/documentation/fundamentals/data-binding