473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

INotifyPropertyChanged Bug ?

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
}
Jan 18 '07 #1
2 2259
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.

"John Greenwood" <jo************@bhpit.co.ukwrote in message
news:2d********************************@4ax.com...
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
}

Jan 18 '07 #2
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.

On Thu, 18 Jan 2007 09:34:31 -0600, "AMDRIT" <am****@hotmail.com>
wrote:
>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.

"John Greenwood" <jo************@bhpit.co.ukwrote in message
news:2d********************************@4ax.com.. .
>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
}
Jan 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: gwolinsky | last post by:
I originally posted this in the WindowsForms group but that may not have been the best place to post it. So, I am reposting here. -------------------------------------- I am trying to...
4
by: Betina | last post by:
Hi, I've created a Person-Object which implemts INotifyPropertyChanged. This Person-Object contains an Address-Object which implements INotifyPropertyChanged too. Then I create a...
1
by: Hardeek Thakkar | last post by:
Dear friends, In my current project I am using the CustomCollections with the help of BindingList<Tgeneric class to store the database records instead using DataSet objects as offline database...
1
by: Gokul | last post by:
Hi, I'm using indexed properties in an object which acts as the binding source. How can I implement INotifyPropertyChanged for that object so that when the indexed property is updated, binding...
0
by: Gerrit | last post by:
Hello, On http://msdn2.microsoft.com/en-us/library/ms184414.aspx is an example of the implementation of INotifyPropertyChanged Interface. I work with the C# example. I have tried this sample,...
0
by: liruiyt | last post by:
This time I found a thorny problem, for I'm a new. When I binding an object to the page in WPF, and the object has a Property of XmlDocument Datatype. And I bind an element of the...
5
by: jehugaleahsa | last post by:
Hello: I have a mapping class that can take generic data objects and locate/ add/remove/update DataRows in a DataTable. I associate them using attributes: TableAttribute and ColumnAttribute. I...
2
by: Dinsdale | last post by:
We have created a object library that implements the INotifyPropertyChanged.PropertyChanged to bubble changes up to higher level classes. For instance, we have a person class that can have...
2
by: nelmr | last post by:
Okay say we have a class that implements INotifyPropertyChanged and a property that has code in the setter to raise the PropertyChanged event. On the GUI side we have a text box with the code: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.