Partilhar via


DependencyProperty.UnsetValue Field

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

Specifies a static value that is used by the property system rather than nulla null reference (Nothing in Visual Basic) to indicate that the property exists, but does not have its value set by the property system.

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

Syntax

'Declaration
Public Shared ReadOnly UnsetValue As Object
public static readonly Object UnsetValue

Field Value

Type: System.Object
The sentinel value for an unset value.

Remarks

UnsetValue is a sentinel value that is used for scenarios where the property system is unable to determine a requested DependencyProperty value. UnsetValue is used rather than nulla null reference (Nothing in Visual Basic), because nulla null reference (Nothing in Visual Basic) could be a valid property value, as well as a valid (and frequently used) default value. UnsetValue should not be returned out of GetValue. When you call GetValue on a dependency property on a DependencyObject, one of the following applies:

  • A dependency property has a default value established, and that value is returned.

  • Some other value was established by the property system, and the default value is no longer relevant. For details, see Dependency Property Value Precedence.

ReadLocalValue returns UnsetValue when the requested property has not been locally set.

Important noteImportant Note:

Do not register a dependency property with the specific default value of UnsetValue. This will be confusing for property consumers and will have unintended consequences within the property system.

Examples

The following example checks for an existing local value with ReadLocalValue. If there is a local value, as indicated by not returning UnsetValue, then the existing local value is removed by calling ClearValue.

public static bool ClearSetProperty(DependencyObject targetObject, DependencyProperty targetDP)
{
    if (targetObject == null || targetDP == null)
    {
        throw new ArgumentNullException();
    }
    object localValue = targetObject.ReadLocalValue(targetDP);
    if (localValue == DependencyProperty.UnsetValue)
    {
        return false;
    }
    else
    {
        targetObject.ClearValue(targetDP);
        return true;
    }
}

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.