CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2000
    Location
    Belgium, Bruges
    Posts
    146

    NotifyPropertyChanged VS DependencyObjects

    Hi, i'm a WPF newbie. I'm trying to learn it as a matter of fact :-)

    If you have binding between a textbox.text and the name property of your employee class, then you have to add code if you want the textbox.text property to be updated when another process changes the name property. You can do this by letting the employee class implement the INotifyPropertyChanged and let the binded properties know Name has changed.

    You can also do this by letting the Employee class inherit from DependencyObject. If you then use the SetValue in your Name property, you get the same effect as with notifypropertychanged.

    Now my question: what is then the difference between the 2 (i know you have extra capabilities with DependencyProperties, zo when should you use notifyPropertyChanged?) Here i have the code that does 2 times the same, but in a different way:

    class Employee : INotifyPropertyChanged
    {
    private string name;

    public string Name
    {
    get { return name; }
    set {
    this.name = value;
    this.NotifyPropertyChanged("Name");// Notify others }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string name)
    {
    if (PropertyChanged != null)
    PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    #endregion


    OR:

    class Employee : DependencyObject
    {
    private string name;

    public static readonly DependencyProperty NameProperty =
    DependencyProperty.Register("Name", typeof(string), typeof(Employee));

    public string Name
    {
    get { return (string) GetValue(NameProperty); }
    set {
    SetValue(NameProperty, value);
    }
    }

    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+) Arjay has a reputation beyond repute (3000+)
    Join Date
    Aug 2004
    Posts
    10,337

    Re: NotifyPropertyChanged VS DependencyObjects

    Take some metrics to be sure, but I'd be the 2nd approach has more overhead.

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width