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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:09:48+00:00 2026-06-18T01:09:48+00:00

I’m working on a small WPF application in which I have a combobox that

  • 0

I’m working on a small WPF application in which I have a combobox that is bound to an ObservableCollection in the code behind:

public Molecule CurrentMolecule { get; set; }
public ObservableCollection<string> Formulas { get; set; }

public MainWindow()
{
     CurrentMolecule = new Molecule();
     Formulas = new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
     DataContext = this;

     InitializeComponent();
}

<ComboBox x:Name="cmbFormula" ItemsSource="{Binding Path=Formulas}" SelectionChanged="cmbFormula_SelectionChanged"/>

This works fine to populate my combo box with the CurrentMolecule.FormulasList however if at some point I set CurrentMolecule to a new instance of Molecule the databinding no longer works. Do I need to implement some kind of OnPropertyChanged event so that no matter what the contents of the combo box will stay current with the CurrentMolecule.FormulasList?

  • 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-18T01:09:49+00:00Added an answer on June 18, 2026 at 1:09 am

    You have to implement INotifyPropertyChanged, only then the changes will be updated in UI.

    Here are the modifications that I’ve done to your code.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            private Molecule _CurrentMolecule;
    
            public Molecule CurrentMolecule
            {
                get
                {
                    return _CurrentMolecule;
                }
                set
                {
                    _CurrentMolecule = value;
                    OnPropertyChanged("CurrentMolecule");
                    Formulas =  new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
                }
            }
    
            private ObservableCollection<string> _Formulas;
    
            public ObservableCollection<string> Formulas
            {
                get { return _Formulas; }
                set
                {
                    _Formulas = value;
                    OnPropertyChanged("Formulas");
                }
            }
    
            public MainWindow()
            {
                InitializeComponent();
    
                CurrentMolecule = new Molecule();
                DataContext = this;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
    

    Edit:
    A better approach is to create a ViewModel and then bind it to the DataContext of the Window.

    Define a new class called ViewModel as below. Note you might want to change the namespace

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    
    namespace WpfApplication1
    {
        public class ViewModel : INotifyPropertyChanged
        {
            #region Properties
            private Molecule _CurrentMolecule;
            public Molecule CurrentMolecule
            {
                get
                {
                    return _CurrentMolecule;
                }
                set
                {
                    _CurrentMolecule = value;
                    OnPropertyChanged("CurrentMolecule");
                    Formulas = new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
                }
            }
    
            private ObservableCollection<string> _Formulas;
            public ObservableCollection<string> Formulas
            {
                get { return _Formulas; }
                set
                {
                    _Formulas = value;
                    OnPropertyChanged("Formulas");
                }
            }
            #endregion
    
            #region Constructor
            public ViewModel()
            {
                CurrentMolecule = new Molecule();
            }
            #endregion
    
            #region INotifyPropertyChanged implementation
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }
    

    Modify the MainWindow code behind file as below

    public MainWindow()
    {
        InitializeComponent();
    
        DataContext = new ViewModel();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have an array which has BIG numbers and small numbers in it. I
I used javascript for loading a picture on my website depending on which small
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.