简体   繁体   English

MVVM绑定对象的属性

[英]MVVM Binding a property of object

Have the problem with binding, if I bind string type Property everything work fine, but if I trying to bind in View the object property of Property xaml not upload. 绑定有问题,如果我绑定字符串类型Property,则一切正常,但是如果我尝试绑定,则无法上传Property xaml的object属性。

No any Errors or warnings. 没有任何错误或警告。

Upadated Model 更新模型

Model: 模型:

     public class Part : BindableObject
{
    public int Id { get; set; }
    public string Brand { get; set; }

    public string _BrandImage;
    public string BrandImage
    {
        get { return _BrandImage; }
        set
        {
            _BrandImage = value;
            OnPropertyChanged();
        }
    }

    public string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            OnPropertyChanged();
        }
    }
    public string Article { get; set; }
    public string Mfg { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
    public List<Offer> Offers { get; set; }   


        };

            return result;
        }

    }    

ViewModel: ViewModel:

public class PartDetailViewModel :  BindableBase
    {
        private Part part;
        public Part Part
        {
            get { return part; }
            set { SetProperty(ref part, value); }
        }   

        public PartDetailViewModel(INavigationService navigationService) 
        {
            Part = part.GetPartById();
        }   
    }

You need to inherit 'BindableObject' and notify the view about the property changed inside the setter 'set {}' for the properties that you are binding in the view. 您需要继承“ BindableObject”,并将要绑定到视图的属性通知给视图有关在setter“ set {}”内更改的属性。 I have modified your code below for Name property, you can change based on your requirements for other fields. 我已经在下面为“名称”属性修改了您的代码,您可以根据对其他字段的要求进行更改。

public class Part : BindableObject
{
    public int Id { get; set; }
    public string Brand { get; set; }
    public string BrandImage { get; set; }

    private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            OnPropertyChanged();
        }
    }
    public string Article { get; set; }
    public string Mfg { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
    public List<Offer> Offers { get; set; }
}

从模型中删除此属性,多数民众赞成在帮助

 public List<Offer> Offers { get; set; }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM