日期:2014-05-18  浏览次数:21110 次

WPF:求教如何注销一个控件上的指定事件?
比如如何注销一个按钮btn上的Click事件?

------解决方案--------------------
C# code

 static void Main(string[] args)
        {
            System.Windows.Forms.Button btn = new System.Windows.Forms.Button();
            btn.Click += new EventHandler(btn_Click);
            btn.Click += new EventHandler(btn_Click2);
            btn.Click += new EventHandler(btn_Click3);
            PropertyInfo pi = btn.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
            EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);
            FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
            Delegate d = ehl[fieldInfo.GetValue(null)];
            foreach (Delegate del in d.GetInvocationList())
                Console.WriteLine(del.Method.Name);
        }

        static void btn_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Click1");
        }

        static void btn_Click2(object sender, EventArgs e)
        {
            Console.WriteLine("Click2");
        }

        static void btn_Click3(object sender, EventArgs e)
        {
            Console.WriteLine("Click3");
        }