Reputation: 13407
This works fine:
EventManager.RegisterClassHandler(typeof(DataGrid),
DataGrid.SelectionChangedEvent,
new RoutedEventHandler(ScrollToSelectedItem));
void ScrollToSelectedItem(object sender, RoutedEventArgs e)
This compiles but gives at runtime the exception: Handler type is mismatched:
EventManager.RegisterClassHandler(typeof(DataGrid),
DataGrid.SelectionChangedEvent,
new Action<object, RoutedEventArgs>(
(object sender, RoutedEventArgs e1) =>
ScrollToSelectedItem(sender, e1, false)));
void ScrollToSelectedItem(object sender, RoutedEventArgs e, bool jump)
The third parameter of RegisterClassHandler
is declared as Delegate handler
.
Is it implicitly assumed that the handler is not only a delegate but a RoutedEventHandler
delegate?
Then why has RegisterClassHandler
not a more accurate signature?
EDIT just discovered that I can solve this via:
RoutedEventHandler htrue = (o, ea) => ScrollToSelectedItem(o, ea, true);
RoutedEventHandler hfalse = (o, ea) => ScrollToSelectedItem(o, ea, false);
void ScrollToSelectedItem(object sender, RoutedEventArgs e, bool jump)
EventManager.RegisterClassHandler(typeof(DataGrid),
DataGrid.SelectionChangedEvent, hfalse);
Upvotes: 1
Views: 424
Reputation: 203828
Just because two different delegates have the same signature doesn't make them the same, for the same reason that the following two types aren't the same:
public class Foo { public int n; }
public class Bar { public int n; }
You can't assign a Foo
to a Bar
just because they have the same instance fields.
Action
is a type, just like RoutedEventHandler
, and there is no implicit conversion between them.
Then why has RegisterClassHandler not a more accurate signature?
As to why it doesn't have a more defined signature, I couldn't say, I didn't design the class. It does sound like it's not very well designed though, given your confusion. It probably should have had a specific delegate as the required parameter.
Upvotes: 2
Reputation: 398
Delegate (uppercase) is a class. delegate (lowercase) is a type.
Action<> returns a delegate (lowercase) which does not cast to Delegate (uppercase) as far as I can tell.
Upvotes: 0