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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:02:35+00:00 2026-05-23T00:02:35+00:00

I have two ComboBoxes, A & B, each bound to an Observable Collection. Each

  • 0

I have two ComboBoxes, A & B, each bound to an Observable Collection. Each has a SelectionChanged trigger is attached which is intended to catch when the user changes a selection. The trigger passes the selection to a Command.

The collections implement INotifyPropertyChanged in that, in the Setter of each, an NotifyPropertyChanged event is fired. This is needed (in the MVVM approach) to notify the UI (the View) that the ComboBox’s contents have changed.

The two ComboBoxes are interdependent – changing the selection in A causes B to be repopulated with new items.

Now, the problem is that B’s SelectionChanged trigger fires in response to its collection being repopulated (as well as the user changing a selection). Due to the complexity of the code in the Command this is a huge waste of resources.

I could in theory stop this by not raising the NotifyPropertyChanged event when B’s collection is set (because, looking at the Call Stack, this is what seems to cause the SelectionChanged trigger to fire), however the MVVM approach depends on this to keep the UI refreshed.

Any suggestions?

  • 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-23T00:02:36+00:00Added an answer on May 23, 2026 at 12:02 am

    Why does ComboB need a SelectionChanged event? You can just bind the selected item directly into a property on the VM.

    The way i have tackled this previously was to bind ComboA’s selected item into the VM. In the setter for that property, i recalculate the available items for ComboB and assign them to another property on the VM, and ComboB’s ItemsSource is bound to this property. Of course that property will notify (using INotifyPropertyChanged), but nothing else needed to be done, my ComboB did not have a SelectionChanged event. By using this method i didn’t need a SelectionChanged on ComboA either, which keeps the view’s code behind nice and sparse – everything is handled in the VM and regular databinding takes care of the rest.

    Edit:

    Here is an example of adjusting the required lists from within the property setters:

    public class MyViewModel : INotifyPropertyChanged
    {
    
        //ItemsSource of ComboA is bound to this list
        public List<SomeObject> ComboAList
        {
            get { return _comboAList; }
            set { _comboAList = value; }
        }
    
        //ItemsSource of ComboB is bound to this list
        public List<SomeObject> ComboBList
        {
            get { return _comboBList; }
            set 
            {
                _comboBList = value;
                OnPropertyChanged("ComboBList");
            }
        }
    
        //ItemsSource of the dataGrid is bound to this list
        public List<SomeObject> DataGridList
        {
            get { return _datagridList; }
            set
            {
                _datagridList = value;
                OnPropertyChanged("DataGridList");
            }
        }
    
        //SelectedItem of ComboA is bound to this property
        public SomeObject FirstSelectedItem
        {
            get { return _firstSelectedItem; }
            set
            {
                _firstSelectedItem = value;
                RefreshListForComboB();
            }
        }
    
        //SelectedItem of ComboB is bound to this property
        public SomeObject SecondSelectedItem
        {
            get { return _secondSelectedItem; }
            set
            {
                _secondSelectedItem = value;
                RefreshListForDataGrid();
            }
        }
    
    
    
        private void RefreshListForComboB()
        {
            //do whatever is necessary to filter or create a list for comboB
            ComboBList = doSomethingThatReturnsAListForComboB();
        }
    
        private void RefreshListForDataGrid()
        {
            //do whatever is necessary to filter or create the list for the DataGrid
            DataGridList = doSomethingThatReturnsAListForDataGrid();
        }
    
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
    
        private List<SomeObject> _comboAList, _comboBList, _datagridList;
        private SomeObject _firstSelectedItem, _secondSelectedItem;
    }
    

    And here is a slightly different way to do it, using a PropertyChange event handler on the VM, this simply changes where the list updating happens. This is arguably a better way of doing it than the first sample as it means the property setters don’t have side effects:

    public class MyViewModel : INotifyPropertyChanged
    {
    
        public MyViewModel()
        {
            this.PropertyChanged += new PropertyChangedEventHandler(MyViewModel_PropertyChanged);
        }
    
        private void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "FirstSelectedItem":
                    RefreshListForComboB();
                    break;
    
                case "SecondSelectedItem":
                    RefreshListForDataGrid();
                    break;
            }
        }
    
        //ItemsSource of ComboA is bound to this list
        public List<SomeObject> ComboAList
        {
            get { return _comboAList; }
            set { _comboAList = value; }
        }
    
        //ItemsSource of ComboB is bound to this list
        public List<SomeObject> ComboBList
        {
            get { return _comboBList; }
            set 
            {
                _comboBList = value;
                OnPropertyChanged("ComboBList");
            }
        }
    
        //ItemsSource of the dataGrid is bound to this list
        public List<SomeObject> DataGridList
        {
            get { return _datagridList; }
            set
            {
                _datagridList = value;
                OnPropertyChanged("DataGridList");
            }
        }
    
        //SelectedItem of ComboA is bound to this property
        public SomeObject FirstSelectedItem
        {
            get { return _firstSelectedItem; }
            set
            {
                _firstSelectedItem = value;
                OnPropertyChanged("FirstSelectedItem");
            }
        }
    
        //SelectedItem of ComboB is bound to this property
        public SomeObject SecondSelectedItem
        {
            get { return _secondSelectedItem; }
            set
            {
                _secondSelectedItem = value;
                OnPropertyChanged("SecondSelectedItem");
            }
        }
    
    
    
        private void RefreshListForComboB()
        {
            //do whatever is necessary to filter or create a list for comboB
            ComboBList = doSomethingThatReturnsAListForComboB();
        }
    
        private void RefreshListForDataGrid()
        {
            //do whatever is necessary to filter or create the list for the DataGrid
            DataGridList = doSomethingThatReturnsAListForDataGrid();
        }
    
    
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
    
        private List<SomeObject> _comboAList, _comboBList, _datagridList;
        private SomeObject _firstSelectedItem, _secondSelectedItem;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have two comboBoxes (comboBoxFromAccount and comboBoxToAccount). Each has the same datasource, which
I have two comboboxes on an form. Each has the values Yes and No.
I have two related ComboBoxes ( continents, and countries ). When the continents ComboBox
I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
Im using c# .net windows form application. I have two comboboxes A and B
What i have is four comboboxes and two files. If the column matches the
I have two comboBoxes, one that lists the 7 days of the week and
I have 3 tables: Item - which is the DataContext - it has a
I have two seperate WPF Comboboxes both of them bind to the same object

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.