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. OnPropertyChangedFreezable 重写实现使用内部信息来确定属性是否为子属性,并为任何一种情况提供适当的基类逻辑。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.

在对象的整个生存期内,可能会多次调用 OnPropertyChangedOnPropertyChanged is potentially invoked many times during the life of an object. 因此,如果你重写特定属性的元数据,然后为单个属性附加 CoerceValueCallbackPropertyChangedCallback 函数,则可以为整个属性系统获得更好的性能。Therefore, 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. 否则,将明显禁用整个 WPFWPF 属性系统,从而导致报告错误的值。Failure to do this will significantly disable the entire WPFWPF property system, causing incorrect values to be reported.