using System.Windows;
namespace WpfApplication10
{
class MyClass
{
public static readonly RoutedEvent OnPrintEvent =
EventManager.RegisterRoutedEvent("OnPrint", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyClass));
public void AddPrintEventHandler(UIElement o, RoutedEventHandler h)
{
o.AddHandler(OnPrintEvent, h);
}
public void RemovePrintEventHandler(UIElement o, RoutedEventHandler h)
{
o.RemoveHandler(OnPrintEvent, h);
}
public int Age { get; set; }
public string Name { get; set; }
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyClass myClass = new MyClass() { Name = "111", Age = 12 };
myClass.AddPrintEventHandler(this, new RoutedEventHandler(Print));
myClass.AddPrintEventHandler(this, new RoutedEventHandler(Print2));
this.RaiseEvent(new RoutedEventArgs(MyClass.OnPrintEvent, myClass));
}
public void Print(object o, RoutedEventArgs e)
{
MyClass m = e.OriginalSource as MyClass;
MessageBox.Show(m.Name + System.Environment.NewLine + m.Age);
}
public void Print2(object o, RoutedEventArgs e)
{
MyClass m = e.OriginalSource as MyClass;
MessageBox.Show(m.Name + " " + m.Age);
}
}