NotifyPropertyChanged

// See line 55 using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; namespace TestPropertyChanged { class Program { static ObservableCollection<SelectedSchoolList> _SelectedSchoolList = new ObservableCollection<SelectedSchoolList>(); static void Main(string[] args) { _SelectedSchoolList.CollectionChanged += ListItemChanged; _SelectedSchoolList.Add(new SelectedSchoolList()); _SelectedSchoolList[0].SchoolName = "Bach Khoa University"; _SelectedSchoolList.Add(new SelectedSchoolList()); _SelectedSchoolList[1].Score = "4.0"; Console.ReadLine(); } private static void ListItemChanged(object sender, NotifyCollectionChangedEventArgs e) { Console.WriteLine(($"New index of the item changed: {e.NewStartingIndex}")); } } class SelectedSchoolList : INotifyPropertyChanged { private string _schoolName; private string _score; public string SchoolName { get => _schoolName; set { _schoolName = value; NotifyPropertyChanged(); } } public string Score { get => _score; set { _score = value; NotifyPropertyChanged(); } } private ObservableCollection<SelectedStudentList> _electedStudentArray; public ObservableCollection<SelectedStudentList> SelectedStudentArray { get => _electedStudentArray; set { _electedStudentArray = value; NotifyPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; // Add a function to PropertyChanged in constructor public SelectedSchoolList() { this.PropertyChanged += DataChanged; } private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private static void DataChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case "SchoolName": Console.WriteLine(($"Property changed: {e.PropertyName}, new value: {(sender as SelectedSchoolList).SchoolName}")); break; case "Score": Console.WriteLine(($"Property changed: {e.PropertyName}, new value: {(sender as SelectedSchoolList).Score}")); break; case "SelectedStudentArray": Console.WriteLine(($"Property changed: {e.PropertyName}")); break; } } } public class SelectedStudentList { public int StudentID { get; set; } public string StudentName { get; set; } public string IndividualScore { get; set; } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.