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'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
}