#606 – 重用(另一个类中)已经存在的路由事件(Reusing an Existing Routed Event in Your Class)
2017-01-03 11:50
435 查看
当你准备定义一个路由事件的时候,你有两种方法:自己注册一个新的路由事件,或者使用一个已经存在的路由事件。
你可以通过调用一个已经存在的路由事件的AddOwner 方法使用这个事件。
public class MyButton : Button
{
public static readonly RoutedEvent MyTextChangedEvent;
static MyButton()
{
MyTextChangedEvent = TextBoxBase.TextChangedEvent.AddOwner(typeof(MyButton));
}
public event TextChangedEventHandler MyTextChanged
{
add { AddHandler(MyTextChangedEvent, value); }
remove { RemoveHandler(MyTextChangedEvent, value); }
}
protected virtual void OnMyTextChanged()
{
TextChangedEventArgs evargs = new TextChangedEventArgs(MyTextChangedEvent, UndoAction.None);
RaiseEvent(evargs);
}
public MyButton()
{
this.Click += new RoutedEventHandler(MyButton_Click);
}
void MyButton_Click(object sender, RoutedEventArgs e)
{
this.Content = this.Content + ".";
OnMyTextChanged();
}
}
上面的例子中,想在MyButton 类中实现原本存在于TextBoxBase 类中的TextChangedEvent。
原文地址:https://wpf.2000things.com/2012/07/19/606-reusing-an-existing-routed-event-in-your-class/
你可以通过调用一个已经存在的路由事件的AddOwner 方法使用这个事件。
public class MyButton : Button
{
public static readonly RoutedEvent MyTextChangedEvent;
static MyButton()
{
MyTextChangedEvent = TextBoxBase.TextChangedEvent.AddOwner(typeof(MyButton));
}
public event TextChangedEventHandler MyTextChanged
{
add { AddHandler(MyTextChangedEvent, value); }
remove { RemoveHandler(MyTextChangedEvent, value); }
}
protected virtual void OnMyTextChanged()
{
TextChangedEventArgs evargs = new TextChangedEventArgs(MyTextChangedEvent, UndoAction.None);
RaiseEvent(evargs);
}
public MyButton()
{
this.Click += new RoutedEventHandler(MyButton_Click);
}
void MyButton_Click(object sender, RoutedEventArgs e)
{
this.Content = this.Content + ".";
OnMyTextChanged();
}
}
上面的例子中,想在MyButton 类中实现原本存在于TextBoxBase 类中的TextChangedEvent。
原文地址:https://wpf.2000things.com/2012/07/19/606-reusing-an-existing-routed-event-in-your-class/
相关文章推荐
- #154 – Reusing an Existing Dependency Property in Your Class(重用依赖属性)
- There is no READABLE property named 'day' in class 'java.lang.String';You have an error in your SQL
- WPF的自定义控件 依赖属性,DependencyProperty 路由事件RoutedEvent
- [转]Raising An Event From CheckBox In A GridView (GridView中模板表的CheckBox的后台事件处理)
- WPF路由事件RoutedEvent
- #604 – 定义一个路由事件(Defining a New Routed Event)
- #600 – 注册一个路由事件(Registering a Routed Event)
- WPF 路由事件(RoutedEventArgs 事件消息、 RoutedEvent 路由事件、RoutedEventHandler路由事件处理程序、RaiseEvent引发路由事件)
- [转]Raising An Event From CheckBox In A GridView (GridView中模板表的CheckBox的后台事件处理)
- Embed an HTML control in your own window using plain C
- An easy instance of using the template class in C++
- This event supports the .NET Framework infrastructure and is not intended to be used directly from your code?继承自VScrollbar的自定义winform控件,某些事件不触发的问题(Winform控件开发学习)
- Implementing your own base class for user controls in Silverlight 2
- [VB.NET]如何在一个窗体M中判断另一个窗体N中是否存在一个名称为A的事件?
- How one VBA project can use an object declared in the class module of another project
- 创建存储过程错误(已解决):Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server ver
- OSCache的源码编译Cache.java 文件报add(Class, T) in the type EventListenerList is not applicable for the arguments 错误的解决
- 把Sql Server数据库备份文件恢复到另一个已经存在的数据上的脚本
- 'Objects in this class cannot be updated outside an edit session'
- Unable to load a class specified in your ejb-jar.xml: Class bytes found but defineClass()failed