I'm using Caliburn.Micro.Xamarin.Forms MVVM framework in my PCL Xamarin Forms. I don't know how to bind the viewModel from the view in my code-behind xaml.cs
In XAML would be:
<Label Text={Binding Username}/>
But I need to write this in the code-behind so:
Label= new Label{
Text= ....?
};
Anyone can help me?
Although this stuff is fairly easy to find on Google I will lay it out for you.
Create the Label first then bind the Text property.
So use code like:
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("PropertyOnYourViewModel"));
If you have the C# 6 features available to you, also get rid of the nasty magic string and do it like this:
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding(nameof(PropertyOnYourViewModel)));
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