Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caliburn Micro Xamarin Forms Binding Codebehind

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

Example:

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?

like image 351
Marco24690 Avatar asked May 01 '26 19:05

Marco24690


1 Answers

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)));

like image 127
Gerald Versluis Avatar answered May 05 '26 05:05

Gerald Versluis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!