DependencyObject.ClearValue 方法

定義

清除屬性的區域數值。

多載

ClearValue(DependencyProperty)

清除屬性的區域數值。 要清除的屬性是由 DependencyProperty 識別項所指定。

ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。 要清除的屬性是由 DependencyPropertyKey 所指定。

ClearValue(DependencyProperty)

清除屬性的區域數值。 要清除的屬性是由 DependencyProperty 識別項所指定。

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

參數

dp
DependencyProperty

要清除的相依性屬性,該屬性是由 DependencyProperty 物件參考所識別。

例外狀況

範例

下列範例會逐一查看物件上設定本機值的所有屬性,然後呼叫 ClearValue 以清除每個這類屬性的值。

C#
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 和其他版本
產品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7

ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。 要清除的屬性是由 DependencyPropertyKey 所指定。

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

參數

key
DependencyPropertyKey

要清除之相依性屬性的索引鍵。

例外狀況

備註

DependencyPropertyKey識別屬性系統作業的唯讀相依性屬性。 定義唯讀相依性屬性的類別不應該公開具有公用存取權的此金鑰。 公開的索引鍵會提供公用程式代碼路徑,以否定 屬性的唯讀字元,如果 或 之類的 ClearValue SetValue 方法可以在 類別或元件之外呼叫,則參考索引鍵。

呼叫 ClearValue 來清除屬性值不一定會提供相依性屬性中繼資料中指定的預設值。 清除值只會特別清除任何可能已套用的本機值。 如需詳細資訊,請參閱相依性屬性值優先順序

另請參閱

適用於

.NET Framework 4.8 和其他版本
產品 版本
.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
Windows Desktop 3.0, 3.1, 5, 6, 7