Is there a way to use set binding within the label using object initializer in Xamarin Forms
a) Label lb = new Label();
lb.setBinding(----);
b) new Label{
**Set Binding** ???
}
No, you can't use object initializer to set the binding of Controls.
Because (definition from MSDN): Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.
You can't use SetBinding in Object initilaizer because its not a property or an accessible field, but a method.
You have to use SetBinding as follows :
var label = new Label ();
label.SetBinding (Label.TextProperty, "Name");
label.BindingContext = new {Name = "John Doe", Company = "Xamarin"};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With