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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:39:57+00:00 2026-05-22T22:39:57+00:00

I have a silverlight mvvm application that loads main view with 2 user controls

  • 0

I have a silverlight mvvm application that loads main view with 2 user controls loaded into 2 ContentControls, one with listbox showing items and other with edit button. When i click edit button, 2 new user controls load into the ContentControls, one showing data to edit (EditData) and other having Save and Cancel button (EditAction).
When i click save button, it raises an event that is defined in seperate GlobalEvents.cs class like:

public event EventHandler OnSaveButtonClicked;  
public void RaiseSaveButtonClicked()  
{  
  this.OnSaveButtonClicked(this, EventArgs.Empty);  
}

and i subscribe to it in the other user control EditData, because i need to transfer that edited data via custom EventArgs, so i have put in the constructor of it’s ViewModel:

this.globalEvents.OnSaveButtonClicked += (s, e) => SaveData();  

and in the Save data:

public void SaveData()  
{  
    globalEvents.RaiseSaveData(EditedGuy);     
}  

which raises another event that loads previous user controls into their ControlContent and shows edited data in list box. Thats all fine, but whenever i click on edit and then save again, it raises the event twice, and again 3 times, then 4 and so on. How can i make it to be raised only ONE time? I thought it could be because every time i click edit, a new instance of the user control is loaded and i dont know, maybe the subscription to the event stays, so i have tried to paste

this.globalEvents.OnSaveButtonClicked -= (s, e) => SaveData(); 

to the Dispose() method but without success. How can i make this work?

  • 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-22T22:39:58+00:00Added an answer on May 22, 2026 at 10:39 pm

    You can’t use lambdas when you want to unregister from events.

    this.globalEvents.OnSaveButtonClicked += (s, e) => SaveData(); 
    

    This will create one instance – let’s call it instance A – of type EventHandler and add it as a handler.

    this.globalEvents.OnSaveButtonClicked -= (s, e) => SaveData(); 
    

    This will not remove instance A from the event but create a new instance – instance B – and tries to remove it from the event.

    To fix this problem, either create a little method or save that anonymous method in a field:

    class ViewModel
    {
    
        private EventHandler _saveButtonClickedHandler;
        // ...
    
        public ViewModel()
        {
            _saveButtonClickedHandler = (s, e) => SaveData();
            this.globalEvents.OnSaveButtonClicked += _saveButtonClickedHandler;
            // ...
        }
    
        public void Dispose()
        {
            this.globalEvents.OnSaveButtonClicked -= _saveButtonClickedHandler;
            // ...
        }
    
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a silverlight application (MVVM) with a view that will be used by
I have a mvvm(model view viewmodel) silverlight application that has several views that need
I'm writing a Silverlight app using the MVVM pattern. I have a main view
I have an MVVM Silverlight 4 application that holds a list of modules (a
I am using the Business Silverlight application. I have incorporated some MVVM into this
I have a silverlight 2 beta 2 application that accesses a WCF web service.
I have a Silverlight 2 application that is consuming a WCF service. As such,
I have a Silverlight application that is built from a set of Silverlight class
I have a Silverlight application that communications with an ASP.NET backend through WCF. I
I've got this Silverlight Prism application that is using MVVM. The model calls a

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.