如果子类属性发生更改,则会通知父类

时间:2015-09-30 15:49:55

标签: c# mvvm

我必须查看ViewModels,OrganizationContact和PersonalInformationModel。

OrganizationContact使用PersonalInformationModel。

它们的设置如下:

OrganizationContact:

public class OrganizationContact : ViewModelBase
{
    private PersonalInformationModel _contactInfo;

    public PersonalInformationModel ContactInfo
    {
        get
        {
            return _contactInfo;
        }
        set
        {
            _contactInfo = value;
            RaisePropertyChanged(nameof(ContactHeader), "", "", true);
            RaisePropertyChanged(nameof(ContactInfo), null, _contactInfo, true);
        }
    }

    //Generate Header
    public string ContactHeader
    {
        get
        {
            var header = "";

            if (!string.IsNullOrWhiteSpace(ContactInfo.Title?.TitleAbbreviation))
            {
                header += ContactInfo.Title.TitleAbbreviation + " ";
            }
            if (!string.IsNullOrWhiteSpace(ContactInfo.FirstName))
            {
                header += ContactInfo.FirstName + " ";
            }
            if (!string.IsNullOrWhiteSpace(ContactInfo.MiddleInitial))
            {
                header += ContactInfo.MiddleInitial + ". ";
            }
            if (!string.IsNullOrWhiteSpace(ContactInfo.LastName))
            {
                header += ContactInfo.LastName + " ";
            }

            return header;
        }
    }

    public int OrganizationLink { get; set; }

    public string Position { get; set; }

    public int Priority { get; set; }
}

PersonalInformationModel:

public class PersonalInformationModel : ViewModelBase
{
    private string _firstName;
    private string _middleInitial;
    private string _lastName;
    private string _phoneNumber;
    private string _phoneExtension;
    private string _faxNumber;
    private string _email;

    public int PersonalIdentity { get; set; }

    public string FirstName
    {
        get
        {
            return _firstName;
        }
        set
        {
            _firstName = value;
            RaisePropertyChanged(nameof(FirstName), "", _firstName, true);
        }
    }

    public string MiddleInitial
    {
        get
        {
            return _middleInitial;
        }
        set
        {
            _middleInitial= value;
            RaisePropertyChanged(nameof(MiddleInitial),"",_middleInitial,true);
        }
    }

    public string LastName
    {
        get
        {
            return _lastName;
        }
        set
        {
            _lastName = value;
            RaisePropertyChanged(nameof(LastName), "", _lastName, true);
        }
    }

    public string PhoneNumber
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            _phoneNumber = value;
            RaisePropertyChanged(nameof(PhoneNumber), "", _phoneNumber, true);
        }
    }

    public string PhoneExtension
    {
        get
        {
            return _phoneExtension;
        }
        set
        {
            _phoneExtension = value;
            RaisePropertyChanged(nameof(PhoneExtension), "", _phoneExtension, true);
        }
    }

    public string FaxNumber
    {
        get
        {
            return _faxNumber;
        }
        set
        {
            _faxNumber = value;
            RaisePropertyChanged(nameof(FaxNumber), "", _faxNumber, true);
        }
    }

    public string Email
    {
        get
        {
            return _email;
        }
        set
        {
            _email = value;
            RaisePropertyChanged(nameof(Email),"",_email, true);
        }
    }

    public string FullName => $"{FirstName} {LastName}";
}

PersonalInformationModel由其他类使用。

如果OrganizationContact内的任何属性发生变化,PersonalInformationModelContactHeader内的OrganizationContact可能会发生变化,那么我正在寻找{ "name": "propertyType", "in": "query", "description": "Type of home", "required": false, "type": "list", "enum": ["singleFamilyHome", "condo", "farm"] } 的通知方式通知了这一变化。

1 个答案:

答案 0 :(得分:1)

好的,为了得到你想要的东西,你需要做一些事情。首先,在OrganizationContact上设置ContactInfo属性时注册PropertyChanged处理程序:

public PersonalInformationModel ContactInfo
{
    get
    {
        return _contactInfo;
    }
    set
    {
        if (_contactInfo != null)
        {
           _contactInfo.PropertyChanged -= ContactInfo_PropertyChanged;
        }

        _contactInfo = value;

        if (_contactInfo != null)
        {
           _contactInfo.PropertyChanged += ContactInfo_PropertyChanged
        }

        RaisePropertyChanged(nameof(ContactInfo), null, _contactInfo, true);
    }
}

现在,创建您的处理程序。您应该能够在ContactHeader上引发PropertyChanged事件以更新绑定。

void ContactInfo_PropertyChanged(object sender, PropertyChangedEventArgs args)
{
    RaisePropertyChanged(nameof(ContactHeader), "", "", true);
}