使用英语阅读

通过


DependencyObject.ClearValue 方法

定义

清除属性的本地值。

重载

ClearValue(DependencyProperty)

清除属性的本地值。 要清除的属性由 DependencyProperty 标识符指定。

ClearValue(DependencyPropertyKey)

清除只读属性的本地值。 要清除的属性由 DependencyPropertyKey 指定。

ClearValue(DependencyProperty)

清除属性的本地值。 要清除的属性由 DependencyProperty 标识符指定。

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

参数

dp
DependencyProperty

要清除的由 DependencyProperty 对象引用指定的依赖属性。

例外

示例

以下示例循环访问对象上设置了本地值的所有属性,然后调用 ClearValue 以清除每个此类属性的值。

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

注解

通过调用 ClearValue 清除属性值不一定为依赖属性提供依赖属性元数据中指定的默认值。 清除属性仅专门清除可能已应用的任何本地值。 有关详细信息,请参阅依赖属性值优先级

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.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, 10

ClearValue(DependencyPropertyKey)

清除只读属性的本地值。 要清除的属性由 DependencyPropertyKey 指定。

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

参数

key
DependencyPropertyKey

要清除的依赖属性的关键字。

例外

注解

标识 DependencyPropertyKey 属性系统操作的只读依赖属性。 定义只读依赖属性的类不应公开具有公共访问权限的此密钥。 公开的键将提供一个公共代码路径,该路径可否定属性的只读字符,前提是可以在类或程序集外部调用方法(如 ClearValueSetValue ),并引用该键。

通过调用 ClearValue 清除属性值不一定为依赖属性提供依赖属性元数据中指定的默认值。 清除该值仅专门清除可能已应用的任何本地值。 有关详细信息,请参阅依赖属性值优先级

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.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, 10