Silverlight:《Pro Silverlight5》读书笔记 之 Dependency Properties And Routed Event

Dependency Properties And Routed Event

Dependency Properties

Dynamic Value Resolution

As you’ve already learned, dependency properties depend on multiple different services, called property providers. To determine the current value of a property, Silverlight has to decide which one takes precedence. This process is called dynamic value resolution.

  1. Animations. If an animation is currently running and that animation is changing the property value, Silverlight uses the animated value.
  2. Local value. If you’ve explicitly set a value in XAML or in code, Silverlight uses the local value. Remember, you can set a value using the SetValue() method or the property wrapper. If you set a property using a resource or data binding, it’s considered to be a locally set value.
  3. Silverlight styles allow you to configure multiple controls with one rule. If you’ve set a style that applies to this control, it comes into play now.
  4. Property value inheritance. Silverlight uses property value inheritance with a small set of control properties, including Foreground, FontFamily, FontSize, FontStretch, FontStyle, and FontWeight. That means if you set these properties in a higher-level container (such as a Button or a ContentControl), they cascade down to the contained content elements (like the TextBlock that actually holds the text inside).
  5. Default value. If no other property setter is at work, the dependency property gets its default value. The default value is set with the PropertyMetadata object when the dependency property is first created, as explained in the previous section.

One of the advantages of this system is that it’s very economical. For example, if the value of a property has not been set locally, Silverlight will retrieve its value from the template or a style. In this case, no additional memory is required to store the value. Another advantage is that different property providers may override one another, but they don’t overwrite each other. For example, if you set a local value and then trigger an animation, the animation temporarily takes control. However, your local value is retained, and when the animation ends, it comes back into effect.

 

你可能感兴趣的:(silverlight)