Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/how-to-override-metadata-for-a-dependency-property.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

3.4 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Override Metadata for a Dependency Property 03/30/2017
csharp
vb
metadata [WPF], overriding for dependency properties
dependency properties [WPF], overriding metadata for
overriding metadata for dependency properties [WPF]
f90f026e-60d8-428a-933d-edf0dba4441f

How to: Override Metadata for a Dependency Property

This example shows how to override default dependency property metadata that comes from an inherited class, by calling the xref:System.Windows.DependencyProperty.OverrideMetadata%2A method and providing type-specific metadata.

Example

By defining its xref:System.Windows.PropertyMetadata, a class can define the dependency property's behaviors, such as its default value and property system callbacks. Many dependency property classes already have default metadata established as part of their registration process. This includes the dependency properties that are part of the [!INCLUDETLA2#tla_winclient] API. A class that inherits the dependency property through its class inheritance can override the original metadata so that the characteristics of the property that can be altered through metadata will match any subclass-specific requirements.

Overriding metadata on a dependency property must be done prior to that property being placed in use by the property system (this equates to the time that specific instances of objects that register the property are instantiated). Calls to xref:System.Windows.DependencyProperty.OverrideMetadata%2A must be performed within the static constructors of the type that provides itself as the forType parameter of xref:System.Windows.DependencyProperty.OverrideMetadata%2A. If you attempt to change metadata once instances of the owner type exist, this will not raise exceptions, but will result in inconsistent behaviors in the property system. Also, metadata can only be overridden once per type. Subsequent attempts to override metadata on the same type will raise an exception.

In the following example, the custom class MyAdvancedStateControl overrides the metadata provided for StateProperty by MyAdvancedStateControl with new property metadata. For instance, the default value of the StateProperty is now true when the property is queried on a newly constructed MyAdvancedStateControl instance.

[!code-csharpPropertySystemEsoterics#MyStateControl] [!code-vbPropertySystemEsoterics#MyStateControl]
[!code-csharpPropertySystemEsoterics#MyAdvancedStateControl] [!code-vbPropertySystemEsoterics#MyAdvancedStateControl]

See also