I have a model

public class Student : NotificationObject { public string FullName { get { return SecondName + " " + Name[0] + ". " + MiddleName[0] + "."; } } private string _name; public string Name { get { return _name; } set { if (value != null && _name != value) { _name = value; RaisePropertyChanged(nameof(Name)); } } } private string _secondName; public string SecondName { get { return _secondName; } set { if (value != null && _secondName != value) { _secondName = value; RaisePropertyChanged(nameof(SecondName)); } } } private string _middleName; public string MiddleName { get { return _middleName; } set { if (value != null && _middleName != value) { _middleName = value; RaisePropertyChanged(nameof(MiddleName)); } } } ObservableCollection<BooleanWrapper> _attendance; public ObservableCollection<BooleanWrapper> Attendance { get { return _attendance; } set { if (value != null && _attendance != value) { _attendance = value; RaisePropertyChanged(nameof(Attendance)); } } } private Student(){ } public Student(string Name, string SecondName, string MiddleName) { this.Name = Name; this.SecondName = SecondName; this.MiddleName = MiddleName; Attendance = new ObservableCollection<BooleanWrapper>(); } // ... } 

During the application, it becomes necessary to create / add model objects. I do this through a special pair of View-ViewModel.

The problem is that I can’t understand how to wrap properly when editing a model object in ViewModel. If earlier I used the Set () method from ViewModelBase, then bindings (as I think) need to be done to the properties of the model object (and based on the Set signature, properties cannot be passed with the ref keyword). It turns out you need to do something like that?

 public string Name { get { return Student.Name; } set { Student.Name = value; RaisePropertyChanged(nameof(Name)); } } 

If so, then how can the control from the previous View (which presents a collection of model objects) be notified of the change to the FullName property? I think that it’s not quite correct to prescribe:

 //Student.cs private string _name; public string Name { get { return _name; } set { if (value != null && _name != value) { _name = value; RaisePropertyChanged(nameof(Name)); RaisePropertyChanged(nameof(FullName)); } } } private string _secondName; public string SecondName { get { return _secondName; } set { if (value != null && _secondName != value) { _secondName = value; RaisePropertyChanged(nameof(SecondName)); RaisePropertyChanged(nameof(FullName)); } } } private string _middleName; public string MiddleName { get { return _middleName; } set { if (value != null && _middleName != value) { _middleName = value; RaisePropertyChanged(nameof(MiddleName)); RaisePropertyChanged(nameof(FullName)); } } } 
  • Why is this wrong? - Sergey
  • Well, I think that 2 identical strings, differing only in the name of the property, can somehow be transformed into one. - bodynar
  • one
    Not. And there is nothing wrong with that. - Sergey

0