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 6549609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:06:49+00:00 2026-05-25T12:06:49+00:00

Consider the reference Josh Smith’ article WPF Apps With The Model-View-ViewModel Design Pattern ,

  • 0

Consider the reference Josh Smith’ article WPF Apps With The Model-View-ViewModel Design Pattern, specifically the example implementation of a RelayCommand (In Figure 3). (No need to read through the entire article for this question.)

In general, I think the implementation is excellent, but I have a question about the delegation of CanExecuteChanged subscriptions to the CommandManager‘s RequerySuggested event. The documentation for RequerySuggested states:

Since this event is static, it will
only hold onto the handler as a weak
reference. Objects that listen for
this event should keep a strong
reference to their event handler to
avoid it being garbage collected. This
can be accomplished by having a
private field and assigning the
handler as the value before or after
attaching to this event.

Yet the sample implementation of RelayCommand does not maintain any such to the subscribed handler:

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }
}
  1. Does this leak the weak reference up to the RelayCommand‘s client, requiring that the user of the RelayCommand understand the implementation of CanExecuteChanged and maintain a live reference themselves?
  2. If so, does it make sense to, e.g., modify the implementation of RelayCommand to be something like the following to mitigate the potential premature GC of the CanExecuteChanged subscriber:

    // This event never actually fires.  It's purely lifetime mgm't.
    private event EventHandler canExecChangedRef;
    public event EventHandler CanExecuteChanged
    {
        add 
        { 
            CommandManager.RequerySuggested += value;
            this.canExecChangedRef += value;
        }
        remove 
        {
            this.canExecChangedRef -= value;
            CommandManager.RequerySuggested -= value; 
        }
    }
    
  • 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-25T12:06:50+00:00Added an answer on May 25, 2026 at 12:06 pm

    I too believe this implementation is flawed, because it definitely leaks the weak reference to the event handler. This is something actually very bad.
    I am using the MVVM Light toolkit and the RelayCommand implemented therein and it is implemented just as in the article.
    The following code will never invoke OnCanExecuteEditChanged:

    private static void OnCommandEditChanged(DependencyObject d, 
                                             DependencyPropertyChangedEventArgs e)
    {
        var @this = d as MyViewBase;
        if (@this == null)
        {
            return;
        }
    
        var oldCommand = e.OldValue as ICommand;
        if (oldCommand != null)
        {
            oldCommand.CanExecuteChanged -= @this.OnCanExecuteEditChanged;
        }
        var newCommand = e.NewValue as ICommand;
        if (newCommand != null)
        {
            newCommand.CanExecuteChanged += @this.OnCanExecuteEditChanged;
        }
    }
    

    However, if I change it like this, it will work:

    private static EventHandler _eventHandler;
    
    private static void OnCommandEditChanged(DependencyObject d,
                                             DependencyPropertyChangedEventArgs e)
    {
        var @this = d as MyViewBase;
        if (@this == null)
        {
            return;
        }
        if (_eventHandler == null)
            _eventHandler = new EventHandler(@this.OnCanExecuteEditChanged);
    
        var oldCommand = e.OldValue as ICommand;
        if (oldCommand != null)
        {
            oldCommand.CanExecuteChanged -= _eventHandler;
        }
        var newCommand = e.NewValue as ICommand;
        if (newCommand != null)
        {
            newCommand.CanExecuteChanged += _eventHandler;
        }
    }
    

    The only difference? Just as indicated in the documentation of CommandManager.RequerySuggested I am saving the event handler in a field.

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

Sidebar

Related Questions

I'm going to use a small example for reference. Consider a project with: inner_definitions.o
Consider the following HTML. If I have a JSON reference to the <button> element,
Please consider this code. Is it using Circular Reference? If not why am I
Consider three django models: AA, BB and CC. AA has an M2M reference to
I have a question regarding the passing of a map by reference. Let's consider
I am exploring a pattern where you can save a global reference to the
Consider the following as a reference implementation: /* calculates (a * b) / c
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the need to develop a lightweight desktop DB application on the Microsoft platforms.
Consider: List<String> someList = new ArrayList<>(); // add "monkey", "donkey", "skeleton key" to someList

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.