Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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;

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top