Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6738249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:20:41+00:00 2026-05-26T11:20:41+00:00

I am trying out custom routed events, but I get a TargetInvocationException when compiling

  • 0

I am trying out custom routed events, but I get a TargetInvocationException when compiling with an Attached Event Handler.

I have the following code inside custom control EventRaiserControl:

public static readonly RoutedEvent KickedEvent = EventManager.RegisterRoutedEvent("KickedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(EventRaiserControl));

public event RoutedEventHandler Kicked
{
    add
    { this.AddHandler(KickedEvent, value); }

    remove
    { this.RemoveHandler(KickedEvent, value); }
}

private void btn1_Click(object sender, RoutedEventArgs e)
{
    RaiseEvent(new RoutedEventArgs(KickedEvent));
}

I then have the following XAML in my main window:

<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">
    <local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>
</StackPanel>

With the following handlers in the MainWindow code behind:

private void StackPanel_Kicked(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Caught Kicked Event at Panel level.");
}

private void EventRaiserControl_Kicked(object sender, RoutedEventArgs e)
{
    Console.WriteLine("Caught Kicked Event at Control level.");
}

My code works fine with this handler:

<local:EventRaiserControl Kicked="EventRaiserControl_Kicked"/>

But fails with TargetInvocationException the moment I add the attached handler:

<StackPanel local:EventRaiserControl.Kicked="StackPanel_Kicked">

Can somebody help? What am I missing / misusing?

Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:20:42+00:00Added an answer on May 26, 2026 at 11:20 am

    At first I was surprised why it is, but I saw the reason after coding as your code. Just change

    EventManager.RegisterRoutedEvent("KickedEvent"....
    

    to

    EventManager.RegisterRoutedEvent("Kicked"....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have a problem when trying to create events for a custom control in
I'm trying out the new Storyboard feature in iOS 5/XCode, but I can't get
I'm trying out this nice way of customizing grouped UITableViewCell backgrounds: http://code.coneybeare.net/how-to-make-custom-drawn-gradient-backgrounds I've implemented
I am trying to figure out how to add a custom control to the
I'm trying to create a custom file/check in check out script for external hardrives,
Trying out the examples found on pleac.sf.net , I'm not able to get an
I'm trying out the following query: SELECT A,B,C FROM table WHERE field LIKE 'query%'
I am building a custom view and trying to figure out how to integrate
I am trying out Workflow 4.0 and hoping to write a custom activity to
Trying out the following, will result in error. How do I retrieve the values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.