Dependency Property Error

I am studying dependency properties. I read a lot of posts and books, but still I do not know.

Below is the program I wrote to find out. Some error in this, please help in resolving. I have some questions.

  • The main use of the user-defined Dependency property element for change notification?
  • I found the "IsDefaultProperty" code for Button in a WPF text editor. Does this mean that the IsDefault 'property is a dependency property?
  • Why did they show this code? Does this mean, inside, in the Button class, that it is defined like this? (Did they show internal code?) Or did they show 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 an error:

 > Error: GetValue and SetValue does not exist in the current context 
+6
source share
2 answers

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

No, it can also be organized using the implementation of the INotifyPropertyChanged class. Dependency properties to provide change notifications, but the real rationale is something else. See Why dependency properties? and How economical is WPF economical?

I found the IsDefaultProperty code for Button in a WPF text editor. Does this mean the IsDefault property - a dependency property?

Yes. The "FooBarProperty" field naming is a WPF convention for defining dependency properties; you can check the IsDefaultProperty to make sure that this is indeed a dependency property, and even IsDefault has a section on “Dependency Property Information”.

Why did they show this code? Does this mean, inside, in the Button class, is it defined this way? (Did they show internal code?) Or did they show how to define as custom?

I'm not sure what “this” code is, but yes, the property is almost certainly defined as in Button (“almost” because I'm not sure what you mean).

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

This is because you are not affiliated with DependencyObject .

+14
source

DependencyProperty instances must be defined in DependencyObject instances. So your class should consist of DependencyObject or one of its subclasses. You can extract many types from this in WPF, including Button .

So, for your code to work in this case, you should use:

 public class Contact : DependencyObject { // ... 

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

+12
source

Source: https://habr.com/ru/post/915267/


All Articles