How to bind TextStyle using MVVMCross?

I would like to bind the TextStyle property of a TextView using If-Else ValueCombiner in Android. I tried the following but failed 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've tested similar binding with the Text property and it worked fine, so I'm guessing it is looking for something other than a string?

+3


source to share


2 answers


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

Add the following to your setup file (I have two custom binding 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 Style is obviously Text and TextColor ):



<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 (it basically checks if my text is empty or not, if it is, it will be in bold as my converter will add the 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!

+2


source


Here's an example of a text color that Stewart was helping someone else. In MvvmCross, how do I set the binding properties



Using this, you should be able to reverse engineer the way you do this for text style.

0


source







All Articles