0
votes

Im building my first app with Xamarin and im trying to bind a Label to a viewModel like this:

.xaml

                <Label x:Name="lbl_WelcomeMessage" HorizontalOptions="Center"/>

Viewmodel

class LoginModel
{
    public string username { get; set; }
    public string password { get; set; }
    public Label welcomeLabel { get; set; }
    public ICommand loginCommand { get; set; } 


    public LoginController()
    {
        loginCommand = new Command(Login);
    }
}

I succeeded in binding the text properties of the entries, but is it possible to bind the label with name "lbl_WelcomeMessage" to welcomeLabel?

1
Label is a UI element. It really doesn't belong in a ViewModel.Jason
Thank you for your comment. you are making a good point. Im trying to edit multiple properties of the label through the viewmodel (im starting to think im using it more like an Controller like in MVC). instead of getting the entire label should i bind the properties that i want to change individually?Manwolkje
yes, bind each propertyJason

1 Answers

0
votes

First in your YourPage.xaml you have to set Binding to Label. Something like this:

<Label Text={Binding PropertyName}></Label>

After that in your Content Page code behind you have to set BindingContext property in your Content Page constructor. Something like this:

public partial class YourPage : ContentPage
{
    LoginModel loginModel = new LoginModel();
    public YourPage()
    {
        BindingContext = loginModel
    }
}

I hope this will help to you.