您的位置:首页 > 其它

#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/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Routed Event
相关文章推荐