Click here to Skip to main content
15,891,372 members
Articles / Desktop Programming / WPF

Working with ObservableCollection<T>

Rate me:
Please Sign up or sign in to vote.
4.56/5 (25 votes)
17 Dec 2009CPOL3 min read 139.1K   3.3K   51  
One of the most useful classes when working with WPF can be found in the System.ComponentModel namespace, namely the ObservableCollection
using ViewableCollectionDemo.ViewModels.ObjectModel;

namespace ViewableCollectionDemo.ViewModels.Application
{
    /// <summary>
    /// This class describes one contact, in a presentation-friendly manner.
    /// </summary>
    public class ContactViewModel : ObservableObject
    {
        private string _firstName = "New";
        /// <summary>
        /// This property stores the contact's first name.
        /// </summary>
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    base.RaisePropertyChanged("FirstName");
                }
            }
        }

        private string _lastName = "Contact";
        /// <summary>
        /// This property stores the contact's last name
        /// </summary>
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    base.RaisePropertyChanged("LastName");
                }
            }
        }

        /// <summary>
        /// Returns a System.String representing this contact.
        /// </summary>
        /// <returns>A string formatted to have Last Name, First Name.</returns>
        public override string ToString()
        {
            return string.Format("{0}, {1}", this.LastName, this.FirstName);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer N/A
Canada Canada
Sanjay was weaned on VB6, then was dragged kicking and screaming into the world of .NET development, with VB.NET. He was pleasantly surprised, and quickly migrated existing projects to reap the benefits of the massive .NET libraries.

Quite a while ago, he started a pet project that involved fading in and out controls (as well as other animations) in a Windows Forms application. After weeks of hard work, he finally realized that there had to be a better way.

He found WPF.

He changed his main programming language to C#, and has never looked back.

Comments and Discussions