MvvmCross Bind to UIButton.TitleLabel.Text
Asked Answered
O

4

8

I am attempting to bind to the text property of TitleLabel on a UIButton using MvvmCross for Xamarin.iOS. Here's what I have so far...

set.Bind(btnFoo).For(btn => btn.TitleLabel.Text).To(vm => vm.BtnFooText);

I've also tried...

set.Bind(btnFoo.TitleLabel).For(lbl => lbl.Text).To(vm => vm.BtnFooText);

Neither of which seem to work. I appreciate the help!

Orangery answered 27/6, 2013 at 0:25 Comment(1)
Possible duplicate of Fluent Bindings and UIButton titlesMei
B
9

For debugging issues, enabling trace may help - see MvvmCross Mvx.Trace usage

For binding a property on a fixed pre-existing subcontrol of a subcontrol then this approach should work:

set.Bind(sub.subSub).For(c => c.PropertyName).To(vm => vm.Foo);

However, that won't continue to work if the sub control then changes its sub control at any point. For those cases, look at custom bindings - eg see http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

For the specific case of a uibutton, you can just bind its "Title" - see Fluent Bindings and UIButton titles

Bilk answered 27/6, 2013 at 6:7 Comment(2)
AwesomeSauce! I sure appreciate the help! I also have a need to update images on the UI. Looks like Custom Binding is the answer to all of my questions. I appreciate your feedback and patience. I'm off to dive into the custom binding pool.Orangery
The label on a button can be tricky - if you find that clicking the button causes the bound label to reset back to the one set in the XIB, try this syntax instead: set.Bind(myButton).For("Title").To(vm => vm.PropertyToBindTo); My understanding is that it invokes a binding written specifically for the UIButton.Title scenario.Yorkist
G
15

The easiest way to bind a UIButton Title:

set.Bind(btnFoo).For("Title").To(vm => vm.BtnFooText);
Globose answered 2/5, 2017 at 6:5 Comment(1)
I think this is the best answer, since this uses the custom binder provided by MvvmCross out of box, and serves exactly the purpose of what the OP is trying to achieve.Septate
B
9

For debugging issues, enabling trace may help - see MvvmCross Mvx.Trace usage

For binding a property on a fixed pre-existing subcontrol of a subcontrol then this approach should work:

set.Bind(sub.subSub).For(c => c.PropertyName).To(vm => vm.Foo);

However, that won't continue to work if the sub control then changes its sub control at any point. For those cases, look at custom bindings - eg see http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

For the specific case of a uibutton, you can just bind its "Title" - see Fluent Bindings and UIButton titles

Bilk answered 27/6, 2013 at 6:7 Comment(2)
AwesomeSauce! I sure appreciate the help! I also have a need to update images on the UI. Looks like Custom Binding is the answer to all of my questions. I appreciate your feedback and patience. I'm off to dive into the custom binding pool.Orangery
The label on a button can be tricky - if you find that clicking the button causes the bound label to reset back to the one set in the XIB, try this syntax instead: set.Bind(myButton).For("Title").To(vm => vm.PropertyToBindTo); My understanding is that it invokes a binding written specifically for the UIButton.Title scenario.Yorkist
H
7

For me UIButton binding to TitleLabel doesn't work. I came up with custom binding which works great and way flexible:

Apply binding:

  set.Bind(FinishedButton).For(UIButtonTextBinding.Property).To(v => v.FinishActionText);

Binding code:

public class UIButtonTextBinding : MvxTargetBinding
{
    public const string Property = "ButtonText";

    protected UIButton View
    {
        get { return Target as UIButton; }
    }

    public UIButtonTextBinding(UIButton target)
        : base(target)
    {
    }

    public override void SetValue(object value)
    {
        var view = View;
        if (view == null)
            return; 

        var stringValue = value as string;
        view.SetTitle(stringValue, UIControlState.Normal);
    }

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

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}
Hertha answered 20/6, 2015 at 18:50 Comment(0)
M
0

You can use:

set.Bind(btnFoo).For(btn => btn.BindTitle()).To(vm => vm.BtnFooText);

adding the following using:

using MvvmCross.Binding.iOS;
Mignon answered 31/7, 2020 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.