I. Binding to Object

1. Binding data using ObjectDataProvider

AC:Let’s say there is a CLR based data bound object that you are trying to implement. For example a collection of Tool objects, e.g.

  • An Object called Tool that contains a bunch of properties that we want to bind (in this case it just contains a description)
  • A Collection of Tool objects(ToolsCollection); for example to populate a list box etc
  • A Factory to get the reference of ToolsCollection objects which have collection<Tools>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CLRDataBinding
{
    public class Tool : INotifyPropertyChanged
    {
        private string _description = "";
        public string Description
        {
            get
            {
                return _description;
            }
            set
            {
                if (_description != value)
                {
                    _description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }
        public Tool(string description)
        {
            _description = description;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

    }
}
View Code

相关文章: