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?
Asked
Active
Viewed 603 times
3 Answers
1
I swear I tried this before upgrading to f# 4 and silverlight 4 and it didn't work but now it does...

PhilBrown
- 2,949
- 7
- 34
- 53
0
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
- 187,081
- 35
- 232
- 306
-
The PageIndexChanged event only accepts a delegate of one arg and it must be of type EventArg – PhilBrown Jun 10 '10 at 21:14
-
@philbrowndotcom: Umm... where are you getting that from? Its an event it has the signature `public event EventHandler
PageIndexChanged` hence it accepts a delegate of `void (object, EventArgs)` – AnthonyWJones Jun 10 '10 at 21:35 -
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 Jun 10 '10 at 23:37
-
@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 Jun 10 '10 at 23:51
0
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
- 2,949
- 7
- 34
- 53