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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:27:39+00:00 2026-06-15T19:27:39+00:00

I have following code. So basically it executes command ( DelegateCommand based on weak

  • 0

I have following code.

So basically it executes command (DelegateCommand based on weak reference delegates), when Selector.SelectionChanged event is raised.

    public static readonly DependencyProperty SelectionCommandProperty
        = DependencyProperty.RegisterAttached(
            "SelectionCommand",
            typeof(ICommand),
            typeof(CommonUtilities),
            new PropertyMetadata(null, OnSelectionCommandPropertyChanged));

    private static void OnSelectionCommandPropertyChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = d as Selector;
        var command = e.NewValue as ICommand;
        if (selector != null && command != null)
        {
            selector.SelectionChanged
                += (o, args) => command.Execute(selector.SelectedItem);
        }
    }

    public static ICommand GetSelectionCommand(DependencyObject d)
    {
        return d.GetValue(SelectionCommandProperty) as ICommand;
    }

    public static void SetSelectionCommand(DependencyObject d, ICommand value)
    {
        d.SetValue(SelectionCommandProperty, value);
    }

Note that the context is static.

Does this cause leak? I can guess that it doesnt because as far as I know, the anonymous handler would be in effect until the scope of all “outer” variables (i.e. selector, command here) is not applicable for GC. Once they are GCed which would happen when the View (that has selector) and ViewModel (that is supplying command) are unloaded from parent GUI, the anonymous delegate would also be unhooked.

Am I right here?

  • 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-06-15T19:27:40+00:00Added an answer on June 15, 2026 at 7:27 pm

    Here are the references in this example:

    • View:
      • Selector
    • ViewModel:
      • ICommand
    • Selector:
      • Anonymous delegate
      • ICommand
    • Anonymous delegate:
      • Selector
      • ICommand

    This means the view and view-model can be garbage collected, leaving the Selector and ICommand alive.

    The garbage collector is capable of dealing with circular references; so even though the Selector references the delegate, and the delegate references the Selector these can still be garbage collected.

    The ICommand however, will be kept alive as long as this anonymous delegate is kept alive, which is determined solely by the lifetime of the Selector instance. As long as the Selector is being garbage collected, the delegate and ICommand will eventually be garbage collected too.

    So in simple situations, no, your code does not cause a leak.

    There is however a situation where your code does leak handlers, I am assuming your view-model has a property like so:

    public ICommand OnSelectionChanged
    {
        get { return _onSelectionChanged; }
        private set 
        { 
            _onSelectionChanged = value;
            RaisePropertyChanged("OnSelectionChanged");
        }
    }
    

    Which is then bound in the view, if you change the value of this OnSelectionChanged command, your attached property will leak event handlers, as you never unsubscribe the delegate which executes the old command.

    So rather than just one command being executed, all of the previous values of this property will be executed.

    I would go for an implementation more like the following:

    private static void OnSelectionCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = d as Selector;
    
        if (selector != null)
        {
            var oldCommand = e.OldValue as ICommand;
            var newCommand = e.NewValue as ICommand;
            if(oldCommand == null && newCommand != null)
            {
                selector.SelectionChanged += OnSelectionChanged;
            }
            else
            {
                selector.SelectionChanged -= OnSelectionChanged;
            }
        }
    }
    
    private static void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var selector = (Selector)sender;
        var command = GetSelectionCommand(selector);
        if(command != null)
        {
            command.Execute(selector.SelectedItem);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an issue with rails involving javascript. Basically, I have the following code:
I'm struggling with the following code. Basically, I have a class Foo and nested
I have the following code, which basically takes values from a database and populates
I have the following code that basically is a input form as I have
So I have the following code which i basically just a JSON string I
Basically I have the following code: import multiprocessing import time class MyProcess(multiprocessing.Process): def __init__(self,
I currently have the following code. Basically, just a login page and a modal
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have following code <div id=main> <div id=one> </div> <div id=two> </div> <div id=three>
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using

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.