【问题标题】:Xamarin Forms Binding entire label to propertyXamarin Forms将整个标签绑定到属性
【发布时间】:2018-09-09 14:15:23
【问题描述】:

我正在使用 Xamarin 构建我的第一个应用程序,并尝试将标签绑定到 viewModel,如下所示:

.xaml

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

视图模型

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

我成功绑定了条目的文本属性,但是是否可以将名为“lbl_WelcomeMessage”的标签绑定到welcomeLabel?

【问题讨论】:

  • 标签是一个 UI 元素。它确实不属于 ViewModel。
  • 感谢您的评论。你说得很好。我试图通过视图模型编辑标签的多个属性(我开始认为我使用它更像是 MVC 中的控制器)。我应该绑定要单独更改的属性,而不是获取整个标签?
  • 是的,绑定每个属性

标签: c# data-binding xamarin.forms


【解决方案1】:

首先在 YourPage.xaml 中,您必须将 Binding 设置为 Label。像这样的:

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

在后面的内容页面代码中,您必须在内容页面构造函数中设置 BindingContext 属性。像这样的:

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

我希望这会对你有所帮助。

【讨论】:

    猜你喜欢
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多