【6月日新计划24】WPF入门-ObservableCollection<T>的使用

105 阅读2分钟

1. Introduction

Offical Website : learn.microsoft.com/en-us/dotne…

1.Properties

Count

Gets the number of elements actually contained in the Collection<T>.

Item[Int32]

Gets or sets the element at the specified index.

Items

Gets a IList<T> wrapper around the Collection<T>.

2.Method

Add(T)

Adds an object to the end of the Collection<T>.

BlockReentrancy()

Disallows reentrant attempts to change this collection.

CheckReentrancy()

Checks for reentrant attempts to change this collection.

Clear()

Removes all elements from the Collection<T>.

ClearItems()

Removes all items from the collection.

Contains(T)

Determines whether an element is in the Collection<T>.

CopyTo(T[], Int32)

Copies the entire Collection<T> to a compatible one-dimensional Array, starting at the specified index of the target array.

Equals(Object)

Determines whether the specified object is equal to the current object.

GetEnumerator()

Returns an enumerator that iterates through the Collection<T>.

GetHashCode()

Serves as the default hash function.

GetType()

Gets the Type of the current instance.

IndexOf(T)

Searches for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>.

Insert(Int32, T)

Inserts an element into the Collection<T> at the specified index.

InsertItem(Int32, T)

Inserts an item into the collection at the specified index.

MemberwiseClone()

Creates a shallow copy of the current Object.

Move(Int32, Int32)

Moves the item at the specified index to a new location in the collection.

MoveItem(Int32, Int32)

Moves the item at the specified index to a new location in the collection.

OnCollectionChanged(NotifyCollectionChangedEventArgs)

Raises the CollectionChanged event with the provided arguments.

OnPropertyChanged(PropertyChangedEventArgs)

Raises the PropertyChanged event with the provided arguments.

Remove(T)

Removes the first occurrence of a specific object from the Collection<T>.

RemoveAt(Int32)

Removes the element at the specified index of the Collection<T>.

RemoveItem(Int32)

Removes the item at the specified index of the collection.

SetItem(Int32, T)

Replaces the element at the specified index.

ToString()

Returns a string that represents the current object.

2. Basic Method

1.Foreach

        public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
        
        public void PrintEvery()
        {
            foreach (Student student in datalist)
            {
                System.Diagnostics.Trace.WriteLine("---------element:" + student);
            }
        }

2.Add

        public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
 
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Student s = new Student();
            s.SID = txtid.Text;
            s.SName = txtname.Text;
            s.SAddress = txtaddress.Text;
            datalist.Add(s);
        }

3.Update

        public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
 
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in datalist)
            {
                if (item.SID == txtid.Text)
                {
                    item.SName = txtname.Text;
                    item.SAddress = txtaddress.Text;
                }
            }
        }

4.Delete

        public ObservableCollection<Student> datalist = new ObservableCollection<Student>();
 
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            var ca = (Student)this.dgStudent.SelectedItem;
            datalist.Remove(ca);
        }