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
2010-06-10 20:55:16
The PageIndexChanged event only accepts a delegate of one arg and it must be of type EventArg
PhilBrown
2010-06-10 21:14:53
@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
2010-06-10 21:35:46
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
2010-06-10 23:37:24
@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
2010-06-10 23:51:56
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
2010-06-11 13:52:01
A:
I swear I tried this before upgrading to f# 4 and silverlight 4 and it didn't work but now it does...
PhilBrown
2010-06-28 20:32:20