-
October 17th, 2016, 03:01 PM
#1
INotifyPropertyChanged why it does not to inform and does not need eventhandlerproce
This code is from a Winform VS2012, works well.
But normally, If I want to use an event of a datagriedview or listview, I have to inform about the event and than the eventhandle procedure is expected, like within the first example.
But if it comes to INotifyPropertyChanged, none of it is needed. I cant see, neither understand why. I searchd around, even considering that INotify is an interface, but finally havnt found any explication.
First example has a code from Winform(Load event, button and datagridview.doubleclickevent)
Second example is class Customer, with PropertyChangedEventHandler
Code:
public partial class Form1 : Form
{
private BindingList<Customer> myBind = null;
private Customer myCust= null;
public Form1()
{
myBind = new BindingList<Customer>();
myCust = new Customer();
myCust = myBind.AddNew();
InitializeComponent();
this.dataGridView1.CellDoubleClick += dataGridView1_CellDoubleClick;
}
private void dataGridView1_CellDoubleClick(Object sender, DataGridViewCellEventArgs e)
{
//[...]
}
private void button1_Click(object sender, EventArgs e)
{
myCust.Company = "new Company";
myCust.PosteCode = 1111456;
}
private void Form1_Load(object sender, EventArgs e)
{
myCust.Company = "AnyCompany";
myCust.PosteCode = 123446;
this.txtCompany.DataBindings.Add("Text", myBind, "Company");
this.txtPostCode.DataBindings.Add("Text", myBind, "PosteCode");
}
}
// Second Example class Customer with PropertyChangedEventHandler
Code:
public class Customer : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string mCompany;
private int mPostCode;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
public string Company
{
get { return mCompany; }
set
{
mCompany = value;
OnPropertyChanged(new PropertyChangedEventArgs("Company"));
}
}
public int PosteCode
{
get { return mPostCode; }
set
{
mPostCode = value;
OnPropertyChanged(new PropertyChangedEventArgs("PosteCode"));
}
}
}
-
October 17th, 2016, 09:45 PM
#2
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
The code that uses the Customer class calls the OnPropertyChanged override. As example of a class that does this is an observable collection.
-
October 18th, 2016, 02:37 PM
#3
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
Well, it does. But within a datagriedview class, there are also OnXx Methodes like "protected virtual void OnPropertyChanged" (maybe another name and already overwritten). Eventhough I have to inform about the event I like to use.
Thats what I miss here.
- Button click,
- Onpropertychanged is called,
- but how it riches the BindingList
Last edited by pschulz; October 18th, 2016 at 03:46 PM.
Reason: sounds better
-
October 18th, 2016, 09:17 PM
#4
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
Not sure what you are asking - if you don't see the code externally then you can infer that the control wires it up internally.
-
October 19th, 2016, 01:05 PM
#5
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
If I want to inform about the event, its like that
myCust.PropertyChanged+=myCust_PropertyChanged;
Changing the name from "myCust" to "objCust" would be enough to became different. So it goes if the name "PropertyChanged" changes to "WightChanged".
Its something BindingList can never know, neither a control
How its possible that control or bindinglist has internally code whichs matchs the situation
Last edited by pschulz; October 19th, 2016 at 01:07 PM.
-
October 19th, 2016, 02:50 PM
#6
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
Originally Posted by pschulz
If I want to inform about the event, its like that
myCust.PropertyChanged+=myCust_PropertyChanged;
Changing the name from "myCust" to "objCust" would be enough to became different. So it goes if the name "PropertyChanged" changes to "WightChanged".
Its something BindingList can never know, neither a control
How its possible that control or bindinglist has internally code whichs matchs the situation
Because your BindingList<> that you assign to the control contains a Customer that implements INotifyPropertyChanged. Read up on BindingList<>, INotifyPropertyChanged and so on in msdn. Also look at code examples.
-
October 19th, 2016, 10:33 PM
#7
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
very well, but the call sequence is this:
- myCust.Company = "new Company";
- OnPropertyChanged(new PropertyChangedEventArgs("Company"));
- protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
maybe thats a kind of answer
"INotifyPropertyChanged" expects that "PropertyChangedEventHandler" is named as "PropertyChanged".
I say a kind of, cause it just makes sure that customer will have same designator as Bindinglist, except the propertyname.
Whatever happend here
Code:
myCust = myBind.AddNew();
still deosnt tell me why I dont need to inform like this
Code:
myCust.PropertyChanged +=myCust_PropertyChanged;
-
October 20th, 2016, 11:06 AM
#8
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
You need to read up on what I suggested. Interfaces and generics exist to solve the types of problems you are asking about. If you learn more about these two, then you will understand how this can work internally with a minimum effort in external code written by a developer (which is a good reason to implement with interfaces and generics in your own code).
Last edited by Arjay; October 20th, 2016 at 11:34 AM.
-
October 20th, 2016, 04:33 PM
#9
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
Is there something I should have post earlier, so this answer would have come straight.
-
October 20th, 2016, 07:58 PM
#10
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
Originally Posted by pschulz
Is there something I should have post earlier, so this answer would have come straight.
I probably could have gotten to that sooner, but I mistakenly assumed you understood about interfaces and generics.
-
October 21st, 2016, 02:12 PM
#11
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
I'm a little surprised, cause its pretty easy to object here.
-
October 22nd, 2016, 05:50 PM
#12
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
raise an objection or raise an argument(better to say).
Someone knows all about this two subjects, but he tooks time, four times, to explain why he cant see the couse of the reaction. What for?
- he wants to proof the moderator(yeah possible)
- or there is even one subject left
cant imagine any else, for doing so(if someone knows about this two... but...)
-
October 22nd, 2016, 07:03 PM
#13
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
If there is one thing I've learned in the years that I've spent donating my time answering forum questions is that you can't please everyone.
-
October 25th, 2016, 03:55 PM
#14
Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp
"...answering forum questions is that you can't please everyone. "
understand the words, but not the meaning in this context. Maybe its just kind of thing, that english is that precisly languange.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|