Howcome get / set in dependency property does nothing?

I created the dependency property as follows:

public partial class MyControl: UserControl { //... public static DependencyProperty XyzProperty = DependencyProperty.Register("Xyz",typeof (string),typeof (MyControl),new PropertyMetadata(default(string))); public string Xyz { get { return (string) GetValue(XyzProperty ); } set { SetValue(XyzProperty , value); } } //... } 

Then bind it to my wpf window and everything works fine.

When I tried to add some logic to the installer, I noticed that it was not called. I change get: set to the point now they look like this:

  get{return null;} set{} 

And it still works! How so? What causes the use of this GetValue / SetValue?

+7
source share
2 answers

The WPF data binding infrastructure directly uses DependencyProperty, the Xyz property is a convenient interface for the programmer.

Take a look at PropertyMetadata in the DependencyProperty.Register call, you can specify the callback that will be executed when the property value changes, this is where you can apply your business logic.

+7
source

DependencyProperty is the repository for XyzProperty. If you access a property through the DependencyProperty interface, it completely bypasses the Get / Set property receiver.

Think of it this way:

 private int _myValue = 0; public int MyValue { get { return _myValue; } set { _myValue = value; } } 

In this case, if I manually set _myValue = 12 , it is obvious that the Install accessory for the MyValue property will not be called; I completely went around it! The same applies to DependencyProperties. The WPF binding system uses DependencyProperty interfaces directly.

+1
source

All Articles