xamarin.forms绑定到类属性不起作用

时间:2018-03-08 16:23:44

标签: xaml xamarin data-binding

在我的XamarinForms项目中,我试图将标签文本绑定到类的属性。当我将对象传递给我的视图时,标签没有被填充。谁能看到我做错了什么?

在我的视图模型中我有

    class ManagerLevelPageViewModel : INotifyPropertyChanged
{
    private UserSelections _MyUserSelections;
    public UserSelections MyUserSelections
    {
        get { return _MyUserSelections; }
        set {
                _MyUserSelections = value;
                NotifyPropertyChanged();
            }
    }

        public ManagerLevelPageViewModel(UserSelections _temp)
    {
        MyUserSelections = _temp;
        MyUserSelections.selectedClientName = _temp.selectedClientName;
        //myUserSelections = _myUserSelections;
        //SetValues();
    }

这是班级

public class UserSelections
{
    public int selectedClientId { get; set; }
    public string selectedClientName { get; set; }
    public string selectedClientShortCode { get; set; }

    public decimal selectedClientPL { get; set; }

    public string TopdayIdentifier { get; set; }
}

这是view.cs

        ManagerLevelPageViewModel vm;

    public ManagerLevelPage (UserSelections _myUserSelections)
    {
        vm = new ManagerLevelPageViewModel(_myUserSelections);
        InitializeComponent ();
        BindingContext = vm;

        DownloadData();
    }

最后这里是xaml

                <Label Text="{Binding MyUserSelections.ClientName}"/>

通知属性已更改

        public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

0 个答案:

没有答案