How to: Apply the PropertyNameChanged Pattern

 

The following code example demonstrates how to apply the PropertyNameChanged pattern to a custom control. Apply this pattern when you implement custom controls that are used with the Windows Forms data binding engine.

    // This class implements a simple user control 
    // that demonstrates how to apply the propertyNameChanged pattern.
    [ComplexBindingProperties("DataSource", "DataMember")]
    public class CustomerControl : UserControl
    {
        private DataGridView dataGridView1;
        private Label label1;
        private DateTime lastUpdate = DateTime.Now;

        public EventHandler DataSourceChanged;

        public object DataSource
        {
            get
            {
                return this.dataGridView1.DataSource;
            }
            set
            {
                if (DataSource != value)
                {
                    this.dataGridView1.DataSource = value;
                    OnDataSourceChanged();
                }
            }
        }

        public string DataMember
        {
            get { return this.dataGridView1.DataMember; }

            set { this.dataGridView1.DataMember = value; }

        }

        private void OnDataSourceChanged()
        {
            if (DataSourceChanged != null)
            {
                DataSourceChanged(this, new EventArgs());
            }
        }
       
        public CustomerControl()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.label1 = new System.Windows.Forms.Label();
            this.dataGridView1.ColumnHeadersHeightSizeMode = 
                System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.dataGridView1.Location = new System.Drawing.Point(19, 55);
            this.dataGridView1.Size = new System.Drawing.Size(350, 150);
            this.dataGridView1.TabIndex = 1;
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(19, 23);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(76, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Customer List:";
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGridView1);
            this.Size = new System.Drawing.Size(350, 216);
            
        }
    }

To compile the previous code example:

  • Paste the code into an empty code file. You must use the custom control on a Windows Form that contains a Main method.

How to: Implement the INotifyPropertyChanged Interface
Change Notification in Windows Forms Data Binding
Windows Forms Data Binding

Show: