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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:18:37+00:00 2026-05-12T17:18:37+00:00

I have a ViewModel that encapsulates some properties that are being edited in an

  • 0

I have a ViewModel that encapsulates some properties that are being edited in an options dialog. I can’t actually save them to the settings until they hit the Ok button, which will end up calling Commit on this particular ViewModel.

A single property in my ViewModel looks like this:

public bool SomeProperty
{
    get
    {
        return m_SomeProperty;
    }
    set
    {
        if (m_SomeProperty != value)
        {
            m_SomeProperty = value;
            NotifyPropertyChanged("SomeProperty");
        }
    }
}
private bool m_SomeProperty = Properties.Settings.Default.SomeProperty;

So the normal implementation for Commit would be to do this:

public void Commit()
{
    Properties.Settings.Default.SomeProperty = m_SomeProperty;
    // Add other properties here...
}

This isn’t so bad, but the reason I don’t like this is that if you add a new property, you have to add code for it in two places. I try to avoid that when possible.

At first I thought I could declare a private event called OnCommit and have the Commit method raise that event, and have the code for each property add an event handler for the event and do the writing to the settings there, but I don’t know how to do that without adding the event handlers in the constructor anyway, which doesn’t help the situation.

Any ideas? Does anyone have an elegant way to do what I’m trying to do?

EDIT: Thanks to sixlettervariables for the answer. I took that idea and incorporated it into SoapBox Core and open sourced the result. Check out the Options Dialog to see how it works.

  • 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-12T17:18:37+00:00Added an answer on May 12, 2026 at 5:18 pm

    Perhaps maintain a list of Actions to execute?

    private List<Action> commitActions = new List<Action>();
    
    public bool SomeProperty
    {
        get
        {
            return m_SomeProperty;
        }
        set
        {
            if (m_SomeProperty != value)
            {
                m_SomeProperty = value;
                lock (commitActions)
                {
                    commitActions.Add(
                        () => Properties.Settings.Default.SomeProperty = value);
                }
                NotifyPropertyChanged("SomeProperty");
            }
        }
    }
    

    Then update your Commit code to loop through the actions.

    public void Commit()
    {
        List<Action> commits;
        lock (commitActions)
        {
            commits = new List<Action>(commitActions);
            commitActions.Clear();
        }
    
        foreach (var commit in commits)
        {
            commit();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ViewModel that has inside a Model and some extra properties. There are
I have a viewmodel that contains a number of properties, a SelectList, and an
I have a ViewModel that represents multiple options and implements IDataErrorInfo . This ViewModel
I have a viewmodel that has an EF object as one of its properties.
I have some fields in my ViewModel that I only need for the output
I have a viewmodel that contains a product and SelectList of categories. public class
I have a ViewModel class that contains a list of points, and I am
I have a ViewModel (AbstractContextMenu) that represents my context menu (IContextMenu), and I bind
The default MVVP I have seen has multiple ViewModel objects that are rendered through
I have a textbox that i am binding to the viewmodel's string property. The

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.