万千封印
属性更改时引发事件对于这种情况,您可以创建一个DependencyPropertyWatcher来检测DependencyProperty更改的事件。下面是可以直接使用的工具类。public class DependencyPropertyWatcher<T> : DependencyObject, IDisposable{ public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(object), typeof(DependencyPropertyWatcher<T>), new PropertyMetadata(null, OnPropertyChanged)); public event DependencyPropertyChangedEventHandler PropertyChanged; public DependencyPropertyWatcher(DependencyObject target, string propertyPath) { this.Target = target; BindingOperations.SetBinding( this, ValueProperty, new Binding() { Source = target, Path = new PropertyPath(propertyPath), Mode = BindingMode.OneWay }); } public DependencyObject Target { get; private set; } public T Value { get { return (T)this.GetValue(ValueProperty); } } public static void OnPropertyChanged(object sender, DependencyPropertyChangedEventArgs args) { DependencyPropertyWatcher<T> source = (DependencyPropertyWatcher<T>)sender; if (source.PropertyChanged != null) { source.PropertyChanged(source.Target, args); } } public void Dispose() { this.ClearValue(ValueProperty); }}用法var watcher = new DependencyPropertyWatcher<string>(this.MyWebView, "Source");watcher.PropertyChanged += Watcher_PropertyChanged;private void Watcher_PropertyChanged(object sender, DependencyPropertyChangedEventArgs e){}