4

I am using MvxBindableListView to bind a List<> of data objects to a ListView. The layout I am using for the rows has several TextViews. I am successfully binding the Text property for each of these to a property in my data object, but I have found that I cannot bind to TextColor as that property does not exist in Mono For Android TextViews; instead you have use the SetTextColor() method. So how can I bind a data object property 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'}
        }" />
Askolein
  • 3,250
  • 3
  • 28
  • 40
Jason Steele
  • 1,598
  • 3
  • 23
  • 41

1 Answers1

8

There's an example of adding a custom 2-way binding for "IsFavorite" in the Conference sample - see:

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

For a one-way "source-to-target" custom binding, the code should be a bit simpler - you only need to handle the SetValue - and don't need to invoke FireValueChanged in any event handling code.


For textColor, I'd imagine the binding would look a bit like:

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 would be setup 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've not compiled this example code - when you do get it working, please come back and correct this pseudo-code :)

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • To be honest, we just cut and paste the code exactly as it is and it just worked - not bad for pseudo code! ;) – Jason Steele May 22 '12 at 11:52
  • If you are building these samples inside some sort of SuperSimpleBinding thing, then it would be fab if you could GitHub it :) - assuming GitHub is now a verb :) It would be useful for your internal training as well as for public mvx information too :) – Stuart May 22 '12 at 12:10
  • 1
    Could you give me a clue, how I should deal with the similar situation on Touch and in case I would want to deals with such binding not relatively some property but Action (for instance, like "Button onclick")? I've found MvxTargetBinding class to inherit from, but the situation with event/action handling/binding is not fully clear for me. Thanks! – Agat Jun 19 '13 at 12:32
  • i am also interested to know the same in iOS (touch). can you please help me? – SoftSan Feb 14 '14 at 07:42
  • 3
    Both of your code links point to a 404, any chance you could update this answer? – Rabbit Jan 23 '17 at 17:10