Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CreateDefaultValueCallback delegate

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

Syntax


public delegate object CreateDefaultValueCallback()

Attributes

[Version(0x06020000)]
[WebHostHidden()]

Parameters

This delegate has no parameters.

Return value

The desired default value.

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.

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
        }
    })


Requirements (device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (operating system)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

Custom dependency properties
DependencyProperty.Register
PropertyMetadata

 

 

Show:
© 2015 Microsoft