INotifyPropertyChanged Bug ?

  • Thread starter Thread starter John Greenwood
  • Start date Start date
J

John Greenwood

Hi,

I'm not sure if i'm misunderstanding how INotifyPropertyChanged should
work but it's not working as I expected.

I have a sample program with a very simple 'Customer' class with three
properties code, name & address. All these are bound to a form via a
BindingSource and fire the PropertyChanged event when the property is
modified.

I change one of these properties in code and find that all of the
properties are fetched and rebound to the form. Is this correct ?
What's the point of specifying a property name in the
PropertyChangedEventArgs if all the controls are rebound anyway.

Any help would be much appreciated

John

If it's any help my code is below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NotifyChange
{
public partial class Form1 : Form
{
private Customer _customer;

public Form1()
{
InitializeComponent();
_customer = new Customer();
_customer.Code = "G001";
_customer.Name = "Greenwood Enterpises Ltd";
_customer.Address = "128 Smithey Road";
customerBindingSource.DataSource = _customer;
}

private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("<-- Change Code --");

_customer.Code = "F001";

Console.WriteLine("-- Change Code -->");
}

private void button2_Click(object sender, EventArgs e)
{
Console.WriteLine("<-- Change Name --");

_customer.Name = "Fletcher Business Services Ltd";

Console.WriteLine("-- Change Name -->");
}

}

public class Customer : INotifyPropertyChanged
{

private string _code = "";
private string _name = "";
private string _address = "";

public string Code
{
get
{
Console.WriteLine("Got 'Code' at {0}", DateTime.Now);
return _code;
}
set
{
if (_code != value)
{
_code = value;

NotifyPropertyChanged(" Code");
}
}
}

public string Name
{
get
{
Console.WriteLine("Got 'Name' at {0}", DateTime.Now);
return _name;
}
set
{
if (_name != value)
{
_name = value;

NotifyPropertyChanged("Name");
}
}
}

public string Address
{
get
{
Console.WriteLine("Got 'Address' at {0}",
DateTime.Now);
return _address;
}
set
{
_address = value;
}
}

private void NotifyPropertyChanged(string Name)
{

if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs(Name));
}
}
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}
 
I would think that propertychanged means that it already happened, while
propertychanging gives you the opportunity to stop it.

take a look at how formclosing and formclosed behave.
 
Hi,
Thanks for the response, but the INotifyPropertyChanged is an
interface, providing your own classes with the abilbity to trigger an
event telling the form to rebind a property. Problem is, regardless of
the property specified in the event, all the forms controls are
rebound.
 
Back
Top