2012-05-09 54 views
3

一個完整的例子如何通過代碼

public delegate void mouseup_delegate(object obj, MouseButtonEventArgs args); 

    constructor() 
    { 
     TextBlock text_block = new TextBlock() { Text = "aa" }; 
     Style style = new Style(); 
     //style.Setters.Add(new EventSetter(){Event=TextBlock.MouseUpEvent, Handler=new mouseup_delegate(this.textblockClicked)}); 
     style.Setters.Add(new EventSetter(TextBlock.MouseUpEvent, new mouseup_delegate(this.textblockClicked))); 
     text_block.Style = style; 
    } 

    public void textblockClicked(object sender, MouseButtonEventArgs args) 
    { 
     MessageBox.Show("mouse up"); 
    } 

添加eventsetter但是當我運行應用程序時,一個異常thown:處理器類型無效

什麼不對的代碼?

+0

是否使用此代替工作? '新的EventSetter(TextBlock.MouseUpEvent,新的urlClickDelegate(this.url_click))' –

+0

謝謝,但仍然出現異常。如果我不將url_style(包含EventSetter)分配給TextBlock.Style,則不會拋出異常 –

回答

7

EventSetter預計您提供的代表是MouseButtonEventHandler而不是mouseup_delegate

+0

謝謝,對於我的遲迴復感到抱歉 –