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

  • SEARCH
  • Home
  • 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 918461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:22:44+00:00 2026-05-15T18:22:44+00:00

(Background: I’m porting a WinForms app to WPF in stages. At the present time,

  • 0

(Background: I’m porting a WinForms app to WPF in stages. At the present time, I still have a WinForms main form with an ElementHost that has WPF content in it.)

I’d like my app to be told when certain ApplicationCommands such as Cut, Copy, and Paste change the value of their .CanExecute. I thought it’d be a simple matter of subscribing to a global event like ApplicationCommands.Cut.CanExecuteChanged, but I am noticing some strange behavior where it doesn’t seem to be called consistently.

For example, I created a simplified WinForms test app that just has an ElementHost. I then added a WPF textbox to it and attached CanExecuteChanged handlers to it:

public Form1()
{
   InitializeComponent();

   var tb = new System.Windows.Controls.TextBox {Text = "WPF Inside ElementHost"};

    ApplicationCommands.Cut.CanExecuteChanged += Cut_CanExecuteChanged;
    ApplicationCommands.Cut.CanExecuteChanged +=
        (s, e) => Debug.WriteLine("CanExecute Changed=" + ApplicationCommands.Cut.CanExecute(null, s as IInputElement));

    elementHost1.Child = tb;
}

private void Cut_CanExecuteChanged(object sender, System.EventArgs e)
{
    Debug.WriteLine("CanExecute Method for Cut = " + ApplicationCommands.Cut.CanExecute(null, sender as IInputElement));
}

The bizarre thing is that the handler that uses the inline lambda/delegate is called when I do things like select text in the textbox. However, the one that subscribes using an instance method does not get called.

Additionally, in my more complicated application, I’m not seeing the CanExecute get called at all, even when I use an inline delegate for the handler.

  • 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-15T18:22:45+00:00Added an answer on May 15, 2026 at 6:22 pm

    (In a case of using StackOverflow as a rubber duck, I realized the answer when I finished writing the question)

    While reflector’ing around a bit, I saw that the CanExecuteChanged event simply passes the handler to CommandManager.RequerySuggested. Because this later event is a static event, it uses WeakReference handlers and prunes the list when it goes to raise the event. Since I wasn’t keeping a hard reference around in the class, the GC was detecting that the reference was no longer valid and was getting pruned.

    The solution was simply to add a class member variable that had the EventHandler and then use that member variable reference when attaching the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.