I am learning dependency properties. I read many posts & books but still I am not clear.
The program shown below is which I wrote to learn. Some error in that, please help in resolving. I have questions.
- The main use of custom Dependency property element is for the notification of change?
- I found an ‘IsDefaultProperty’ code for Button in a WPF text book. It means ‘IsDefault‘ property is a dependency property?
- Why they shown that code? It means, internally, in Button class, its defined like that?
(They showed internal code?) or they showed how to define as custom?
Here is my code:
namespace DependencyProperties
{
public class Contact
{
private int id=100;
private string name="shri";
public static readonly DependencyProperty IsPresentProperty;
public int ID
{
get { return id; }
}
public string NAME
{
get { return name; }
}
static Contact()
{
IsPresentProperty = DependencyProperty.Register("IsPresent", typeof(bool),typeof(Contact),new FrameworkPropertyMetadata(false,new PropertyChangedCallback(OnIsPresentChanged)));
}
public bool Present
{
get { return (bool)GetValue(Contact.IsPresentProperty); }
set { SetValue(Contact.IsPresentProperty, value); }
}
private static void OnIsPresentChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
}
}
}
And I see the error:
> Error: GetValue and SetValue does not exist in the current context
No, that can also be arranged by having the class implement
INotifyPropertyChangedas well. Dependency properties to provide change notifications, but the real rationale is something different. See Why dependency properties? and How is the WPF property system economical?Yes. Naming a field “FooBarProperty” is the WPF convention for defining dependency properties; you can check the docs for
IsDefaultPropertyto see that it’s indeed a dependency property, and even theIsDefaultdocumentation has a section called “dependency property information”.I ‘m not sure what “that” code is, but yes, the property is almost certainly defined like that in
Button(“almost” because I ‘m not sure what you are referring to).That’s because you are not deriving from
DependencyObject.