I am trying to mark an event as handled but an exception gets thrown when I try doing so.
I have the following function that gets called on the LostFocus
event of a TextBox
:
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// ...
// do something
// ...
e.Handled = true;
}
However, the following exception gets raised as soon as e.Handled = true
is executed:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll
Additional information: Every RoutedEventArgs must have a non-null RoutedEvent associated with it.
I have tried moving the e.Handled
line before the other code in the function but that doesn't help.
Why is the exception being raised and what are some possible workarounds?
Figured it out. TextBox_LostFocus
was being called from elsewhere in the application with the event parameter being null
.
Hence, for some function calls to TextBox_LostFocus
, e.Handled
didn't work (since e
was null) but worked in some other scenarios (when it was actually called by the framework).