自定义事件、属性、方法

首先自定义事件:

public class DropdownTextBox : Control
    {
    public static readonly RoutedEvent DropdownOpenedEvent = EventManager.RegisterRoutedEvent("DropdownOpened", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(DropdownTextBox));  // 事件系统注册新的路由事件。
     //类型也可以改为RoutedEventHandler   事件名称,事件传阅的方式(向上传阅,向下传阅或不传阅),事件对应的EventHandler的类型,事件拥有者的类型)
public event TextChangedEventHandler TextChanged { add { AddHandler(TextChangedEvent, value); } remove { RemoveHandler(TextChangedEvent, value); } } } 
public static readonly DependencyProperty TextProperty = TextBox.TextProperty.AddOwner(typeof(DropdownTextBox),new FrameworkPropertyMetadata(new PropertyChangedCallback(TextPropertyChanged)));
   
        private static void TextPropertyChanged(DependencyObject d, DependencyPropertyChangedEve
    //参数属性名(该属性名与声明的依赖属性名称"XXXProperty"相比仅仅是少了"Property"后缀,其它完全一样,否则在运行时会报异常),属性的数据类型,属性的拥有者的类型,元数据.
ntArgs e) { DropdownTextBox db = d as DropdownTextBox; db.OnTextChanged(); } /// <summary> /// 获取或设置控件中显示的文本 /// </summary> public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } }
   protected virtual void OnTextChanged()
        {
            RoutedEventArgs args = new RoutedEventArgs(TextChangedEvent, this);
            RaiseEvent(args);
        }

// 有待整理。。。。。。

参考资料 http://www.cnblogs.com/zhouyinhui/archive/2007/10/27/939920.html

原文地址:https://www.cnblogs.com/wlwenjie/p/4497380.html