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.

LocalValueEnumerator 由呼叫所取得的, GetLocalValueEnumerator 可以用來列舉在實例上具有本機設定值的屬性 DependencyObjectThe 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. 例如,透過配置建立寬度的視覺化架構元素將會報告的本機值 ActualWidthFor example, a visual framework element that has an established width through layout will report a local value for ActualWidth. 如果您要取得區域值以重設它們,請檢查 ReadOnly 每個值的屬性識別碼, LocalValueEntry 確認 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.