In MvvmCross, how do I execute custom binding properties

I use MvxBindableListView to bind List<> data objects to ListView . The layout I use for strings has multiple TextView s. I successfully bind the Text property for each of them to a property in my data object, but I found that I can not bind to TextColor , since this property does not exist in Mono for Android TextView s; instead, you use the SetTextColor() method. So how can I bind a property of a data object to a method? Below is the code I tried to use:

  <TextView android:id="@+id/MyValueTextView" android:layout_width="50dp" android:layout_height="20dp" android:layout_gravity="right" android:gravity="center_vertical|right" android:textSize="12sp" local:MvxBind=" { 'Text':{'Path':'MyValue','Converter':'MyValueConverter'}, 'TextColor':{'Path':'MyOtherValue','Converter':'MyOtherConverter'} }" /> 
+3
c # xamarin.android mvvmcross
May 22 '12 at 10:43 a.m.
source share
1 answer

Here is an example of adding a custom two-way binding for "IsFavorite" in a conference example - see:

This example is explained a bit further: MVVMCross Bindings in Android

For a one-way user-binding "source to target" code should be a little simpler - you only need to process SetValue - and you do not need to call FireValueChanged for any event processing code.




For textColor, I would suggest that the binding would look something like this:

 public class MyCustomBinding : MvxBaseAndroidTargetBinding { private readonly TextView _textView; public MyCustomBinding(TextView textView) { _textView = textView; } public override void SetValue(object value) { var colorValue = (Color)value; _textView.SetTextColor(colorValue); } public override Type TargetType { get { return typeof(Color); } } public override MvxBindingMode DefaultMode { get { return MvxBindingMode.OneWay; } } } 

and will be configured with:

  protected override void FillTargetFactories(MvvmCross.Binding.Interfaces.Bindings.Target.Construction.IMvxTargetBindingFactoryRegistry registry) { base.FillTargetFactories(registry); registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("TextColor", (textView) => new MyCustomBinding(textView))); } 

Note. I did not compile this code example - when you earn it, come back and correct this pseudo code :)

+8
May 22 '12 at 10:56
source share



All Articles