sâmbătă, 24 aprilie 2010



Represents an object that participates in the dependency property system.

  • Dependency property is used throughout the platform to enable styling, automatic data binding, animation, etc.
  • One of the primary architectural philosophies used in building WPF was a preference for properties over methods or events.
  • In order to enable two way binding, you need both sides of the bind to support change notification.
  • INotifyPropertyChange allows an object to publish change notifications (it is optional).
  • WPF provides a richer property system, derived from the DependencyObject type. For example, if you have a property that inherits (like FontSize), the system is automatically updated if the property changes on a parent of an element that inherits the value.
  • The GetValue and SetValue methods work within the dependency property system to store and retrieve property values, and trigger notification when such properties change.
  • DependencyProperty.Register method:
    • Sample: public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("StatViewer.Stat.Caption", typeof(string), typeof(Stat));
    • Optionally you can pass metadata that customizes how the property is treated by WPF, as well as callbacks for handling property value changes, coercing values, and validating values
  • The dependency property system provides support for streamlined property storage.
  • A dependency property depends on multiple providers for determining its value
  • It provides change notifications.
  • The dependency property system is based on the concept of inherited property values, and allows cascading changes to various UI element properties.
  • Dependency Properties:
    • The ability for controls to inherit their container element’s properties (such as coordinates, size, and so on)Object-independent storage
    • Tracking the changes made to elements in order to control the state of a control or element for undo commands to be used
    • Complex data binding
    • Constructs for animation routines for a control
  • Sample:
public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("StatViewer.Stat.Caption", typeof(string), typeof(Stat));
public string Caption
get { return GetValue(CaptionProperty) as string; }
set { SetValue(CaptionProperty, value); }
  • By convention all DependencyProperty fields are public, static, and have a Property suffix.
  • Note the static modifier of a DependencyProperty .
  • Steps performed by WPF to calculate the value of a dependency value:
    • Determine the base value
      • There are eight providers that can set the value of most dependency properties, in order from highest to lowest precedence:
        • Local value
        • Style triggers
        • Template triggers
        • Style setters
        • Theme style triggers
        • Theme style setters
        • Property value inheritance
        • Default value
      • Use DependencyPropertyHelper.GetValueSource method to figure out where a given dependency property is getting its current value from
        • it returns a ValueSource
          • it contains IsExpression, IsAnimated, and IsCoerced properties
    • Evaluate (if it is an expression - an object deriving from System.Windows.Expression)
    • Apply animation
      • If one or more animations are running, they have the power to alter the current property value (using the value after step 2 as input) or completely replace it
    • Coerce
      • ProgressBar use CoerceValueCallback delegate to constrain its Value dependency property to a value between its Minimum and Maximum values, returning Minimum if the input value is less than Minimum or Maximum if the input value is greater than Maximum.
    • Validate
      • The potentially-coerced value is passed to a ValidateValueCallback delegate
      • Returning false causes an exception to be thrown, cancelling the entire process.
  • ClearValue() method
    • _button.ClearValue(Button.ForegroundProperty);

Niciun comentariu:

Trimiteți un comentariu