Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency property error

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.

  1. The main use of custom Dependency property element is for the notification of change?
  2. I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?
  3. 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
like image 531
SHRI Avatar asked May 09 '12 08:05

SHRI


People also ask

What is dependency property?

A dependency property can reference a value through data binding. Data binding works through a specific markup extension syntax in XAML, or the Binding object in code. With data binding, determination of the final property value is deferred until run time, at which time the value is obtained from a data source.

How do you declare a dependency property?

Follow the steps given below to define custom dependency property in C#. Declare and register your dependency property with system call register. Provide the setter and getter for the property. Define an instance handler which will handle any changes that occur to that particular instance.

What is difference between dependency property and attached property?

Attached properties are a XAML concept, dependency properties are a WPF concept. In WPF, most UI-related attached properties on WPF types are implemented as dependency properties.

Why is dependency property static?

DependencyProperty has to be static (Class level) because when we create multiple objects of the class which has that property and want to refer the default value for that property the value has to come from that static instance of DependencyProperty.


2 Answers

DependencyProperty instances must be defined on instances of DependencyObject. So, your class must derive from DependencyObject, or one of its subclasses. Many types in WPF derive from this, including Button.

So to get your code working in this case, you must use:

public class Contact : DependencyObject
{
    // ...

This is why you are receiving an error on GetValue and SetValue -- they are defined on DependencyObject.

like image 85
Drew Noakes Avatar answered Sep 27 '22 22:09

Drew Noakes


The main use of custom Dependency property element is for the notification of change?

No, that can also be arranged by having the class implement INotifyPropertyChanged as 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?

I found an 'IsDefaultProperty' code for Button in a WPF text book. It means 'IsDefault' property is a dependency property?

Yes. Naming a field "FooBarProperty" is the WPF convention for defining dependency properties; you can check the docs for IsDefaultProperty to see that it's indeed a dependency property, and even the IsDefault documentation has a section called "dependency property information".

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?

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

Error: GetValue and SetValue does not exist in the current context

That's because you are not deriving from DependencyObject.

like image 27
Jon Avatar answered Sep 27 '22 21:09

Jon