views:

69

answers:

3

I'm using a DataPager control in my silverlight application. I have different pagers for different DataGrids and want to use the same event handler for the PageIndexChanged event for all of them. The delegate must take an EventArgs object as an argument. Can I use this object to "get back" to the control from which the event was fired?

A: 

The sender parameter is the reference to the object that fired the event.

So if you want to get to the DataPager this should do it:-

 DataPager dp = (DataPager)sender;
AnthonyWJones
The PageIndexChanged event only accepts a delegate of one arg and it must be of type EventArg
PhilBrown
@philbrowndotcom: Umm... where are you getting that from? Its an event it has the signature `public event EventHandler<EventArgs> PageIndexChanged` hence it accepts a delegate of `void (object, EventArgs)`
AnthonyWJones
I'm definately missing something here. I'm working in F#, I don't know how much of a difference that makes. All the other handlers I've coded took a RoutedEventHandler as the lone arg and I could use OriginalSource to get the firing control. I've also been using .Add instead of .AddHandler. I'm fairly new to .NET so this stuff is still a little fuzzy to me.
PhilBrown
@philbrowndotcom: Unfortunately F# is "fuzzy" to me. In C# its simple. It may well be in F# as well but you'll need an F# guy to tell you that.
AnthonyWJones
A: 

Here are my two scenarios in F#:

Scenario 1

let pageIndexChanged (args : EventHandler<EventArgs>) = 
    // Do something
    ()

pager.PageIndexChanged.AddHandler(pageIndexChanged)

Which yeilds the error

This expression was expected to have type EventHandler
but here has type
EventHandler -> unit

Scenario 2

let pageIndexChanged (args : EventArgs) = 
    // Do something
    ()

pager.PageIndexChanged.Add(pageIndexChanged)

The compiler accepts this, but I can do nothing with args

PhilBrown
A: 

I swear I tried this before upgrading to f# 4 and silverlight 4 and it didn't work but now it does...

http://connect.microsoft.com/VisualStudio/feedback/details/524645/f-add-event-handler-to-form-anonymous-function-is-required

PhilBrown