Where Dependency Property Value is stored?

Nov 20, 2009 at 4:13 PM

I created the Person class at the end of this post with LastNameProperty dp and I changed its value as follows.

public partial class Window1 : Window

{
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.SetValue(Person.LastNameProperty, "Doe");  // Window object sets dp, no callback called
            MessageBox.Show(this.GetValue(Person.LastNameProperty).ToString());  //shows Doe

            Person person = new Person();
            person.SetValue(Person.LastNameProperty, "Smith");  //Person object sets dp, callback called
            MessageBox.Show(this.GetValue(Person.LastNameProperty).ToString()); // Shows Smith
        }

}

I have no problem undestanding the Person object's use of the dp but my problem is the Windows objec.

Where the Window object stores     Person.LastNameProperty dp? {this.SetValue(Person.LastNameProperty, "Doe");}

Why when the Window object sets the value the callback registed with the property declation not called?

Are any relation of dp property set by the declaring object(Person) and the one set by the Windows object?

 

Thanks

 public class Person : DependencyObject
    {

        public static readonly DependencyProperty LastNameProperty =
            DependencyProperty.Register("LastName", typeof(string), typeof(Person), new PropertyMetadata("No Name", LastNameChangedCallback));

        private static void LastNameChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            Person p = obj as Person;
            MessageBox.Show ( e.NewValue.ToString());
        }
 
        public string LastName
        {
            get
            {
                return (string)GetValue(LastNameProperty);
            }
            set
            {
                SetValue(LastNameProperty, value);
            }
        }
    }