DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs) 方法


每当更新此 DependencyObject 的任何依赖属性的有效值时调用。Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. 更改的特定依赖属性将在事件数据中报告。The specific dependency property that changed is reported in the event data.

 virtual void OnPropertyChanged(System::Windows::DependencyPropertyChangedEventArgs e);
protected virtual void OnPropertyChanged (System.Windows.DependencyPropertyChangedEventArgs e);
abstract member OnPropertyChanged : System.Windows.DependencyPropertyChangedEventArgs -> unit
override this.OnPropertyChanged : System.Windows.DependencyPropertyChangedEventArgs -> unit
Protected Overridable Sub OnPropertyChanged (e As DependencyPropertyChangedEventArgs)



将包含相关依赖属性标识符、类型的属性元数据以及旧值和新值的事件数据。Event data that will contain the dependency property identifier of interest, the property metadata for the type, and old and new values.


此方法通常不用于检测单个属性更改,也不会对每个案例执行属性失效。This method is not intended to generally detect individual property changes or perform invalidations of properties on a per-case basis. OnPropertyChanged 用于修改常规失效模式,前提是了解有关属性的广泛分类的信息。OnPropertyChanged is instead intended for modifications of the general invalidation pattern, if certain information is known about wide classifications of properties. 例如,中的更改 Freezable 可能是的值类型发生更改 Freezable ,也可能是子属性,其中的更改在其他 Freezable 引用内。For example, changes in a Freezable might be changes in the value types of the Freezable, or might be subproperties, where the changes are within other Freezable references. Freezable的重写实现 OnPropertyChanged 使用内部信息来确定属性是否为子属性,并为任意一种情况提供适当的基类逻辑。The Freezable override implementation of OnPropertyChanged uses internal information to determine whether the properties are subproperties, and provides appropriate base class logic for either case.

OnPropertyChanged 在对象的整个生存期内,可能会多次调用。OnPropertyChanged is potentially invoked many times during the life of an object. 因此,如果你重写特定属性的元数据,然后 CoerceValueCallback 为单个属性附加或函数,则可以为整个属性系统获得更好的性能 PropertyChangedCallbackTherefore, you can achieve better performance for the overall property system if you override the metadata of specific properties and then attach CoerceValueCallback or PropertyChangedCallback functions for individual properties. 但是,如果 DependencyObject 包含大量与值相关的依赖项属性,则可以使用此方法,或者,如果包含的逻辑(如呈现行为)必须针对属性失效的几个相关事例重新运行,则应使用此方法。However, you would use this method if a DependencyObject includes a significant number of value-interrelated dependency properties, or if it includes logic such as rendering behavior that must be rerun for several related cases of property invalidations.


始终调用基实现。Always call the base implementation. 如果不这样做,将明显禁用整个 WPF 属性系统,从而导致报告不正确的值。Failure to do this will significantly disable the entire WPF property system, causing incorrect values to be reported.