DependencyObject.ClearValue Method

Definition

Clears the local value of a property.

Overloads

ClearValue(DependencyProperty)

Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier.

ClearValue(DependencyPropertyKey)

Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey.

ClearValue(DependencyProperty)

Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier.

public void ClearValue (System.Windows.DependencyProperty dp);

Parameters

dp
DependencyProperty

The dependency property to be cleared, identified by a DependencyProperty object reference.

Exceptions

Examples

The following example iterates all properties that have local values set on an object, then calls ClearValue to clear the values of each such property.

void RestoreDefaultProperties(object sender, RoutedEventArgs e)
{
    UIElementCollection uic = Sandbox.Children;
    foreach (Shape uie in uic)
    {
        LocalValueEnumerator locallySetProperties = uie.GetLocalValueEnumerator();
        while (locallySetProperties.MoveNext())
        {
            DependencyProperty propertyToClear = locallySetProperties.Current.Property;
            if (!propertyToClear.ReadOnly) { uie.ClearValue(propertyToClear); }
        }
    }
}

Remarks

Clearing the property value by calling ClearValue does not necessarily give a dependency property the default value that is specified in the dependency property metadata. Clearing the property only specifically clears whatever local value may have been applied. For more information, see Dependency Property Value Precedence.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

ClearValue(DependencyPropertyKey)

Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey.

public void ClearValue (System.Windows.DependencyPropertyKey key);

Parameters

key
DependencyPropertyKey

The key for the dependency property to be cleared.

Exceptions

Remarks

A DependencyPropertyKey identifies a read-only dependency property for property system operations. Classes that define read-only dependency properties should not expose this key with public access. A publicly exposed key would provide a public code pathway that negated the read-only character of the property, if methods such as ClearValue or SetValue could be called outside the class or assembly, referencing the key.

Clearing the property value by calling ClearValue does not necessarily give a dependency property the default value that is specified in the dependency property metadata. Clearing the value only specifically clears whatever local value may have been applied. For more information, see Dependency Property Value Precedence.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9