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.