views:

34

answers:

1

I have a class that I want to be able the handle the mouse up event for a grid.

I tried to create it with a static method call like this:

MyDataBinding.BindObjectsToDataGrid(ListOfObjectsToBind, myGrid.MouseUp);

The end goal being that in the method I would assign a delegate to the MouseUp

PassedInMouseUp += myMethodThatWillHandleTheMouseUp;

Looks good here (to me) but the compiler chokes on the first line. It says that I can only use MouseUp with a += or a -=.

Clearly I am going about this the wrong way. How can I get a different class to handle the mouse up with out having to:

  • Pass in the whole grid
  • Expose the method that will be handling the mouse up as a public method.

Or, is this just a limitation and I will have to do one of the above?

+1  A: 

This is not possible without reflection.

Like properties, .Net events compile to a pair of accessor methods - add_EventName and remove_EventName. There is nothing that you can pass as an argument.

SLaks
Maybe I could make a delegate that will add method to the event and then pass that into the method. (To be called later.) Would that work? --- (Better yet I will go try it)
Vaccano
That would work. `MyDataBinding.BindObjectsToDataGrid(ListOfObjectsToBind, h => myGrid.MouseUp += h);`. The parameter would be an `Action<MouseEventHandler>`.
SLaks