DependencyObject.GetLocalValueEnumerator 方法


建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

 System::Windows::LocalValueEnumerator GetLocalValueEnumerator();
public System.Windows.LocalValueEnumerator GetLocalValueEnumerator ();
member this.GetLocalValueEnumerator : unit -> System.Windows.LocalValueEnumerator
Public Function GetLocalValueEnumerator () As LocalValueEnumerator


特定的區域數值列舉值。A specialized local value enumerator.


下列範例會逐一查看在物件上設定本機值的所有屬性,然後呼叫 ClearValue 以清除每個這類屬性的值。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); }
Private Sub RestoreDefaultProperties(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim uic As UIElementCollection = Sandbox.Children
    For Each uie As Shape In uic
        Dim locallySetProperties As LocalValueEnumerator = uie.GetLocalValueEnumerator()
        While locallySetProperties.MoveNext()
            Dim propertyToClear As DependencyProperty = locallySetProperties.Current.Property
            If Not propertyToClear.ReadOnly Then
            End If
        End While
End Sub


區域值SetValue所設定的任何相依性屬性值,而不是屬性系統的其他層面。A local value is any dependency property value that was set by SetValue, as opposed to other aspects of the property system.

藉由呼叫 GetLocalValueEnumerator 取得的 LocalValueEnumerator,可以用來列舉在 DependencyObject 實例上具有本機設定值的屬性。The LocalValueEnumerator obtained by calling GetLocalValueEnumerator can be used to enumerate properties that have a locally set value on a DependencyObject instance. 每個這類屬性在列舉值中都是以 LocalValueEntry 物件表示,其中包含參考特定 DependencyProperty 和其值的屬性。Each such property is represented in the enumerator by a LocalValueEntry object, which has properties that reference the specific DependencyProperty and its values. 列舉本機設定值的這種方法可以用於優化,或用於其他本機值的處理,例如,若要判斷 DependencyObject 的屬性值在清除時將會變更。This technique of enumerating over the locally set values can be used for optimization or for other handling of local values, such as to determine which property values of a DependencyObject would change if they were cleared.


傳回的 LocalValueEnumerator 可能包含唯讀之相依性屬性的 LocalValueEntry 記錄,或是屬性系統所計算之值的相依性屬性。The returned LocalValueEnumerator might contain LocalValueEntry records for dependency properties that are read-only, or dependency properties where values are calculated by the property system. 例如,具有透過配置建立之寬度的 visual framework 元素,將會報告 ActualWidth的區域值。For example, a visual framework element that has an established width through layout will report a local value for ActualWidth. 如果您要取得區域值來重設它們,請檢查每個 LocalValueEntry 之屬性識別碼的 ReadOnly 值,以確認有問題的 DependencyProperty 不是唯讀的。If you are getting local values in order to reset them, check the ReadOnly value on the property identifier of each LocalValueEntry to verify that the DependencyProperty in question is not read-only.