How to add eventetter by code

Full example

    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");
    }

But when I launch the application, an exception is thrown: Handler type is invalid

what is wrong with this code?

+3
source share
1 answer

EventSetterexpects the delegate you provided will be MouseButtonEventHandler, not mouseup_delegate.

+7
source

All Articles