以INotifyPropertyChanged为例:
public class ObservableObject:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
private void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
调用:
public class TestViewModel
{
public ObservableObject ObjectTest
{
get{}
set
{
//.....
ObjectTest.PropertyChanged += .....;
}
}
}
如下另一个例子:
public class ActionMetadata : DependencyObject
{
public event EventHandler VisibilityChanged;
protected void OnVisibilityChanged(EventArgs e)
{
if (VisibilityChanged != null)
{
VisibilityChanged(this, e);
}
}
private static void OnVisibilityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ActionMetadata action = (ActionMetadata)d;
action.OnVisibilityChanged(EventArgs.Empty);
}
}
调用:
ActionMetadata item = new ActionMetadata ();
item.VisibilityChanged += item_VisibilityChanged;