CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2015
    Posts
    59

    Question 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"));
                    }
                }
            }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Aug 2015
    Posts
    59

    Question 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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  5. #5
    Join Date
    Aug 2015
    Posts
    59

    Question 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.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp

    Quote Originally Posted by pschulz View Post
    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.

  7. #7
    Join Date
    Aug 2015
    Posts
    59

    Question 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;

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  9. #9
    Join Date
    Aug 2015
    Posts
    59

    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.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: INotifyPropertyChanged why it does not to inform and does not need eventhandlerp

    Quote Originally Posted by pschulz View Post
    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.

  11. #11
    Join Date
    Aug 2015
    Posts
    59

    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.

  12. #12
    Join Date
    Aug 2015
    Posts
    59

    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...)

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  14. #14
    Join Date
    Aug 2015
    Posts
    59

    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
  •  





Click Here to Expand Forum to Full Width

Featured