Dependeny Properties and auto-implemented properties RRS feed

  • Question

  • All the books I read on Silverlight indicate that a dependency property's getter and setter should look like:

    public bool IsReadOnly
       get { return (bool) GetValue(TextBox.IsReadOnly);}
       set { SetValue(TextBox.IsReadOnly, value);}

    However, when I look in Visual Studio at the definition of the TextBox class,  I see for the dependency property IsReadOnly the following code:

    public bool IsReadOnly { get; set; }

    This appears to be using the "auto-implemented property" definition, which creates for us a hidden private variable, e.g. bool m_IsReadOnly.   However, this is  not what a dependency property needs, as seen from the first example.   Therefore, does the compiler check to see whether an auto-implemented property is a dependency property and if so, generate the "correct" code?  Thanks for your help.



    Sunday, March 8, 2009 8:43 PM


  • Oops!  I think I just answered my own question.   The TextBox class shown by Visual Studio is just a set of   signatures, not a full-blown class definition (for example, it does not have the static constructor shown that sets up all the dependency properties).   Therefore, when it shows that the dependency properties have a "get" and "set" method, this is only a signature, i.e. we are stating that the property does have a getter and setter but not being any more specific.   Hence this is not a use of auto-implemented properties.  It is coincidance that the method signature and the auto-implementation syntax are identical.

    Sunday, March 8, 2009 9:25 PM