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:處理器類型無效
什麼不對的代碼?
是否使用此代替工作? '新的EventSetter(TextBlock.MouseUpEvent,新的urlClickDelegate(this.url_click))' –
謝謝,但仍然出現異常。如果我不將url_style(包含EventSetter)分配給TextBlock.Style,則不會拋出異常 –