DependencyProperty.RegisterAttached Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Registers an attached dependency property with the specified property name, property type, owner type, and property metadata for the property.
static DependencyProperty RegisterAttached(winrt::hstring const& name, TypeName const& propertyType, TypeName const& ownerType, PropertyMetadata const& defaultMetadata);
public static DependencyProperty RegisterAttached(string name, System.Type propertyType, System.Type ownerType, PropertyMetadata defaultMetadata);
function registerAttached(name, propertyType, ownerType, defaultMetadata)
Public Shared Function RegisterAttached (name As String, propertyType As Type, ownerType As Type, defaultMetadata As PropertyMetadata) As DependencyProperty
Parameters
- name
-
String
winrt::hstring
The name of the dependency property to register.
The type of the property, as a type reference (System.Type for Microsoft .NET, a TypeName helper struct for Visual C++ component extensions (C++/CX)).
The owner type that is registering the dependency property, as a type reference (System.Type for Microsoft .NET, a TypeName helper struct for Visual C++ component extensions (C++/CX)).
- defaultMetadata
- PropertyMetadata
A property metadata instance. This can contain a PropertyChangedCallback implementation reference.
Returns
A dependency property identifier that should be used to set the value of a public static read-only field in your class. That identifier is then used to reference the attached property later, for operations such as setting its value programmatically or attaching a Binding.
Examples
This 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. For more examples, see Custom attached properties.
public abstract class AquariumServices : DependencyObject
{
public enum Buoyancy { Floats, Sinks, Drifts }
public static readonly DependencyProperty BuoyancyProperty = DependencyProperty.RegisterAttached(
"Buoyancy",
typeof(Buoyancy),
typeof(AquariumServices),
new PropertyMetadata(Buoyancy.Floats)
);
public static void SetBuoyancy(DependencyObject element, Buoyancy value)
{
element.SetValue(BuoyancyProperty, value);
}
public static Buoyancy GetBuoyancy(DependencyObject element)
{
return (Buoyancy)element.GetValue(BuoyancyProperty);
}
}