MvvmCross binds to UIButton.TitleLabel.Text

I am trying to associate with the TitleLabel text property on UIButton using MvvmCross for Xamarin.iOS. Here is what I still have ...

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

I also tried ...

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

None of them work. I appreciate the help!

+7
source share
3 answers

For debugging problems, enabling tracing can help - see Using MvvmCross Mvx.Trace

To bind a property to a fixed pre-existing subcontrol of a subcontrol, this approach should work:

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

However, this will not continue if the subcontroller then changes its control at any point. In these cases, pay attention to user bindings - for example, see http://slodge.blogspot.co.uk/2013/06/n28-custom-bindings-n1-days-of-mvvmcross.html

For a specific uibutton case, you can simply bind its "Title" - see UIButton Free Bindings and Names

+8
source

For me, binding a UIButton to a TitleLabel does not work. I came up with a custom binding that works great and is 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; } } } 
+6
source

the easiest way to bind a UIButton name:

 set.Bind(btnFoo).For("Title").To(vm => vm.BtnFooText); 
+4
source

All Articles