Why dependency properties in WPF has to be Static

前端 未结 2 1583
借酒劲吻你
借酒劲吻你 2020-12-13 20:36

Why a dependency property has to be Static?

I have seen that it has been already asked in some post here, but I am not able to understand it properly.

It wi

2条回答
  •  北海茫月
    2020-12-13 20:48

    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. So default value for all instance of our class is same and system does not reserve memory for DependencyProperty on each and every instance of that class. This way it reduces the memory footprint.

    Now the next question arise what if we explicitly set the DependencyProperty’s value for objects of the class.(By code or by animation or by style)

    In this case DependencyObject comes into the picture. Any class which has DependencyProperty has to be derived from DependencyObject class (WPF specific class which maintains a collection named EffectiveValues). When user set the DependencyProperty’s value explicitly on the object of the class(By code or by animation or by style), the value is stored in the that EffectiveValues collection which resides within the DependencyObject class and reserve memory there.

提交回复
热议问题