Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add setbinding for Label using Object Initializer in Xamarin Forms

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** ???
}
like image 647
imrohit Avatar asked Aug 01 '16 10:08

imrohit


1 Answers

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"};
like image 161
Rohit Vipin Mathews Avatar answered Nov 01 '22 07:11

Rohit Vipin Mathews