Condividi tramite


DependencyObject Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents an object that participates in the Silverlight dependency property system. DependencyObject is the immediate base class of several other important Silverlight classes, such as UIElement, Geometry, FrameworkTemplate, Style, and ResourceDictionary.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    More...

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public MustInherit Class DependencyObject
public abstract class DependencyObject

The DependencyObject type exposes the following members.

Constructors

  Name Description
Protected methodSupported by Silverlight for Windows Phone DependencyObject Initializes a new instance of the DependencyObject class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object.
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active.
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject.
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set.
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject.
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The DependencyObject class enables Silverlight dependency property system services on its many derived classes. For an overview of the dependency property concept, see Dependency Properties Overview.

The property system's primary function is to compute the values of properties, and to provide system notification about values that have changed. Another key class that participates in the property system is DependencyPropertyDependencyProperty enables the registration of dependency properties into the property system, whereas DependencyObject as a base class enables objects to use and set the dependency properties.

DependencyObject services and characteristics include the following:

  • Dependency property hosting support.

  • Custom dependency property hosting support. You register a dependency property by calling the Register method, and storing the method's return value as a public static field in your DependencyObject class. Also, the ownerType parameter of Register must be the type of your DependencyObject.

  • Attached property hosting support.

  • Custom attached property hosting support. You register a dependency property for the attached property usage by calling the RegisterAttached method, and storing the method's return value as a public static field in your class.

  • Get and set utility methods for values of any dependency properties that exist on the DependencyObject.

  • Dispatcher APIs for advanced threading scenarios.

  • Basic data binding and styling support, by enabling properties to be set as expressions to be evaluated at some later point in an object's lifetime..

It is not typical to derive directly from DependencyObject for most application scenarios. Instead you might derive from a specific control, from one of the control base classes (ContentControl; Control; ItemsControl), from FrameworkElement, or from non-control classes that still participate in UI such as Panel or Grid. Deriving from DependencyObject might be appropriate if you are defining a business or data storage object where you want dependency properties to be active, or if you are creating a service support class that will own attached properties. However, for basic data objects, you might instead implement the interface INotifyPropertyChanged, because by implementing the interface you could base your data on any object. For more information, see Data Binding.

Version Notes

In Silverlight 4 and later, DependencyObject dependency properties can be the target of a data binding. In XAML, you can specify a Binding attribute value for such properties, just like any other case where data binding a property is supported on FrameworkElement. However, DependencyObject does not implement DataContext or SetBinding. Thus, in order to inherit a data context, there must be a containing FrameworkElement that defines that data context. For more information, see Data Binding or Dependency Properties Overview.

If you are specifically targeting Silverlight 3, a property must be a FrameworkElement dependency property in order to be a binding target.

Examples

The following example defines a class that derives from DependencyObject, and defines an attached property along with the identifier field. The scenario for this class is that it is a service class that declares an attached property that other UI elements can set in XAML, and the service potentially acts on the attached property values on those UI elements at run time.

public abstract class AquariumServices : DependencyObject
{
    public enum Bouyancy {Floats,Sinks,Drifts}

    public static readonly DependencyProperty BouyancyProperty = DependencyProperty.RegisterAttached(
      "Bouyancy",
      typeof(Bouyancy),
      typeof(AquariumServices),
      new PropertyMetadata(Bouyancy.Floats)
    );
    public static void SetBouyancy(DependencyObject element, Bouyancy value)
    {
        element.SetValue(BouyancyProperty, value);
    }
    public static Bouyancy GetBouyancy(DependencyObject element)
    {
        return (Bouyancy)element.GetValue(BouyancyProperty);
    }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Other Resources

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    Microsoft.Internal.Pivot.Controls.AlternationConverter
    Microsoft.Internal.Pivot.Controls.InfoPaneFacetDataTemplateSelector
    Microsoft.Internal.Pivot.Interactivity.BindableAttached
    System.Windows.AssemblyPart
    System.Windows.Automation.Peers.AutomationPeer
    System.Windows.Controls.ColumnDefinition
    System.Windows.Controls.DataGridColumn
    System.Windows.Controls.MultiScaleSubImage
    System.Windows.Controls.Pivot.PivotViewerMultiSizeImageSource
    System.Windows.Controls.RowDefinition
    System.Windows.Controls.TextSearch
    System.Windows.Data.CollectionViewSource
    System.Windows.DependencyObjectCollection<T>
    System.Windows.Deployment
    System.Windows.Documents.TextElement
    System.Windows.ExternalPart
    System.Windows.FrameworkTemplate
    System.Windows.Graphics.CompositionMode
    System.Windows.Icon
    System.Windows.InBrowserSettings
    System.Windows.Ink.DrawingAttributes
    System.Windows.Ink.Stroke
    System.Windows.Input.InputMethod
    System.Windows.Input.TouchDevice
    System.Windows.Input.TouchPoint
    System.Windows.Media.Animation.ColorKeyFrame
    System.Windows.Media.Animation.DoubleKeyFrame
    System.Windows.Media.Animation.EasingFunctionBase
    System.Windows.Media.Animation.KeySpline
    System.Windows.Media.Animation.ObjectKeyFrame
    System.Windows.Media.Animation.PointKeyFrame
    System.Windows.Media.Animation.Timeline
    System.Windows.Media.Brush
    System.Windows.Media.CacheMode
    System.Windows.Media.CaptureDevice
    System.Windows.Media.CaptureSource
    System.Windows.Media.Effects.Effect
    System.Windows.Media.Effects.PixelShader
    System.Windows.Media.GeneralTransform
    System.Windows.Media.Geometry
    System.Windows.Media.GradientStop
    System.Windows.Media.ImageSource
    System.Windows.Media.MultiScaleTileSource
    System.Windows.Media.PathFigure
    System.Windows.Media.PathSegment
    System.Windows.Media.Projection
    System.Windows.Media.TimelineMarker
    System.Windows.NotificationWindow
    System.Windows.OutOfBrowserSettings
    System.Windows.PresentationFrameworkCollection<T>
    System.Windows.Printing.PrintDocument
    System.Windows.ResourceDictionary
    System.Windows.SecuritySettings
    System.Windows.SetterBase
    System.Windows.Style
    System.Windows.TriggerAction
    System.Windows.TriggerBase
    System.Windows.UIElement
    System.Windows.VisualState
    System.Windows.VisualStateGroup
    System.Windows.VisualStateManager
    System.Windows.VisualTransition
    System.Windows.Window
    System.Windows.WindowSettings