0

Is there a way to get a reference to a RoutedEvent given its name?

I tried EventManager.GetRoutedEventsForOwner( typeof(ListBox) ).FirstOrDefault( r => r.Name == eventName ) but it return null.

Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50

1 Answers1

1

The ListBox class defines no routed events. The Selector base class does though:

string eventName = "SelectionChanged";
var events = EventManager.GetRoutedEventsForOwner(typeof(System.Windows.Controls.Primitives.Selector)).FirstOrDefault( x=> x.Name == eventName);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I do not know the exact type at compile time, in practice i pass a Type as parameter. How can i get inherited routed events as well? – Mauro Sampietro Apr 13 '17 at 09:35
  • Use reflection: http://stackoverflow.com/questions/23567944/getting-event-via-reflection – mm8 Apr 13 '17 at 09:39
  • Already tried reflection, i extract the event correctly but I found other complications. I use your solution and simply cycle over t.BaseType while != null. Thank You. – Mauro Sampietro Apr 13 '17 at 09:47