Windows Dev Center

DependencyProperty.UnsetValue Field

Specifies a static value that is used by the property system rather than null 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)

public static readonly Object UnsetValue

Field Value

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

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 null, because null 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 properties for Windows Phone 8.

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.

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


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2015 Microsoft