Dependency Properties' key features

In progress...


The purpose of dependency properties is to provide a way to compute the value of a property based on the value of other inputs.

Main features:
Reduced memory footprint
  90% of the properties of a UI control typically stay at its initial values
  The default values are stored once within the dependency property.
Value inheritance
  Value resolution strategy.
    If no local value is set, the dependency property navigates up the logical tree until it finds a value.
  Not good practice cause performance impact.
  The main example is the DataContext.
  Property value inheritance is not the default property system behavior; a property must be established with a particular metadata setting in order to cause that property to initiate property value inheritance on child elements.
Change notification
  used by the databinding
  used by a custom logic

Details:
Value Precedence
  ...
Metadata Overrides
   Call in a constructor the AnyProperty.OverrideMetadata(...)
Pattern (creating)
  Inheritance from the DependencyObject that defines a key, value dictionary that contains local values.
    DependencyObject class, do not natively support INotifyPropertyChanged.
  DependencyProperty.Register() with yours static field.
  A name must always end up with Property - naming convention.
  A property wrapper with no logic.
    XAML ignores the wrapper and calls directly the DependencyObject's SetValue, GetValue.
  propdp - the shortcut initiator in VS.
  Callbacks
    Value Changed Callback
    Coerce Value Callback
      Correction of a new value without throwing an exception
    Validation Callback
      ArgumentException
  Readonly DependencyProperties
    private static readonly DependencyPropertyKey MyPropertyKey = DependencyProperty.RegisterReadonly(...)
    public static readonly DependencyProperty MyProperty =  MyPropertyKey.DependencyProperty;
    public int My
    {
       get { return (bool)GetValue(MyProperty); }
       private set { SetValue(MyPropertyKey, value); }
    }
  Attached Properties
    DependencyProperty.RegisterAttached
    GetPropertyName and SetPropertyName static methods.
    Has no wrapper in an instance.
Attach to events of defined properties
DependencyPropertyDescriptor
  DependencyPropertyDescriptor textDescr = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
  textDescr.AddValueChanged(myTextBox, delegate
How to clear a local value
  DependencyProperty.UnsetValue
  button1.ClearValue( Button.ContentProperty );
Safe Constructor Patterns for DependencyObjects.

Literature
  MSDN
  http://wpftutorial.net/DependencyProperties.html

Comments

Popular posts from this blog

Convention over Git = CoG

jQuery Deferred Object method chain or a Syntactic Sugar

Ctrl+Shift+Right arrow doesn't work in Visual Studio 2019