3

I'd like to bind the TextStyle property of a TextView using the If-Else ValueCombiner in Android. I tried the following, but it fails to create the binding:

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_gravity="center_vertical" android:layout_row="0" android:layout_column="1" android:textSize="28dp" android:gravity="left" android:text="MyText" local:MvxBind="TextStyle If(ShowBold, 'bold', 'normal')" />

I tested similar binding with the Text property and it worked fine, so I guess it's looking for something other than a string?

Martijn00
  • 3,569
  • 3
  • 22
  • 39
BitBot
  • 458
  • 4
  • 14

2 Answers2

2

A bit late but I had the same requirement and just did it now.

Add the following in your setup file (I have two custom bind properties, Style and Summary):

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    base.FillTargetFactories(registry);

    registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Style", textView => new StyleTextViewBinding(textView)));
    registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
}

In my TextView (my custom binding is Style obviously, Text and TextColor are converters):

<TextView
    style="@style/TeamDifficulty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:text="@string/dummy_title"
    local:MvxBind="Text TeamDifficultyText(RowItem.DifficultyEnumCaptain1Int); TextColor TeamDifficultyTextColor(RowItem.DifficultyEnumCaptain1); Style RowItem.DifficultyEnumCaptain1;" />

And the actual code (basically it checks if my text is empty or not, if it is, it will bold it since my converter will add a value to it after):

public class StyleTextViewBinding : MvxAndroidTargetBinding
{
    readonly TextView _textView;

    public StyleTextViewBinding(TextView textView) : base(textView)
    {
        _textView = textView;
    }

    #region implemented abstract members of MvxConvertingTargetBinding
    protected override void SetValueImpl(object target, object value)
    {
        _textView.SetTypeface(_textView.Typeface, Android.Graphics.TypefaceStyle.Bold);            

        if (value != null && Convert.ToBoolean(value))            
            _textView.SetTypeface(_textView.Typeface, Android.Graphics.TypefaceStyle.Normal);                
    }
    #endregion

    public override Type TargetType
    {
        get { return typeof(bool); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

Hope this helps!

PmanAce
  • 4,000
  • 2
  • 24
  • 29
0

Here is an Example of text color that Stuart helped someone else with. In MvvmCross how do I do custom bind properties

Using that you should be able to reverse engineer a way to do this for a text style.

Community
  • 1
  • 1
PkL728
  • 955
  • 8
  • 21