CreateDefaultValueCallback Delegate

Definition

Represents the method that can be invoked as part of a PropertyMetadata constructor to defer definition of a dependency property default value.

public delegate Platform::Object ^ CreateDefaultValueCallback();
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Guid(3605836076, 5557, 20168, 185, 92, 205, 210, 8, 240, 129, 83)]
class CreateDefaultValueCallback : MulticastDelegate
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(3605836076, 5557, 20168, 185, 92, 205, 210, 8, 240, 129, 83)]
public delegate object CreateDefaultValueCallback();
Public Delegate Function CreateDefaultValueCallback() As Object 

Return Value

Object

Platform::Object

IInspectable

The desired default value.

Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

This example shows pseudocode for using CreateDefaultValueCallback in a custom dependency property scenario. Specifically, this creates PropertyMetadata to be used in a DependencyProperty.Register call (not shown).

PropertyMetadata metadata = PropertyMetadata.Create(
    new CreateDefaultValueCallback(() =>
    {
        return new CustomClass() //a DependencyObject
        {
            CustomProperty1 = "default", //DependencyProperty of type String 
            CustomProperty2 = -1; //DependencyProperty of type Int32
        }
    })

Remarks

When you register a custom dependency property, you can supply metadata (a PropertyMetadata value) that provides a default value for all uses of the dependency property. Many times it's adequate to provide that value directly in metadata as an immediate value. This will always work for any value-type value, for example specifying a default value of -1 for an int value property. However, if you want to report a default value for a property that's a reference property, such as a DependencyObject value, you can encounter a threading problem that's related to how dependency properties work. All DependencyObject objects are always created on a UI thread. But it's possible that the thread that registers the property and the thread that eventually uses an instance of the object and tries to access the default aren't the same UI thread. If you provide a CreateDefaultValueCallback in your PropertyMetadata rather than a fixed instance, the default value of the property is created just-in-time and is guaranteed to be acccessible by the thread that's actually using the property. In other words the property as registers deferred what the default value is, but the property as used can now provide a thread-safe default value.

A typical CreateDefaultValueCallback should do nothing more than call a constructor for the reference type that's used as a value, and set properties of that reference type, then return it.

To specify a CreateDefaultValueCallback within a PropertyMetadata instance, you must use PropertyMetadata.Create to create the instance, not a PropertyMetadata constructor.

Applies to

See also