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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:09:43+00:00 2026-05-20T18:09:43+00:00

I got a sample mvvm app. The UI has a textbox, a button and

  • 0

I got a sample mvvm app. The UI has a textbox, a button and a combobox. when I enter something in the textbox and hit the button, the text I enter gets added to an observablecollection. The Combobox is bound to that collection. How do I get the combobox to display the newly added string automaticly?

  • 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-20T18:09:44+00:00Added an answer on May 20, 2026 at 6:09 pm

    As I understand correctly, you want to add an item and select it.
    Here is the example how it can be done using ViewModel and bindings.

    Xaml:

    <StackPanel>
        <TextBox Text="{Binding ItemToAdd}"/>
        <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" />
        <Button Content="Add" Click="Button_Click"/>
    </StackPanel>
    

    ViewModel:

    public class MainViewModel:INotifyPropertyChanged
    {
        public ObservableCollection<string> Items { get; set; }
    
        public string ItemToAdd { get; set; }
    
        private string selectedItem;
    
        public string SelectedItem
        {
            get { return selectedItem; }
            set
            {
                selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }
    
        public void AddNewItem()
        {
            this.Items.Add(this.ItemToAdd);
            this.SelectedItem = this.ItemToAdd;
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    

    The MainViewModel has 3 properties (one for the TextBox and two other for the ComboBox) and the method AddNewItem without parameters.

    The method can be triggered from a command, but there is no standard class for commands, so I will call it from the code-behind:

       ((MainViewModel)this.DataContext).AddNewItem();
    

    So you must explicitly set an added item as selected after you add it to a collection.

    Because the method OnItemsChanged of the ComboBox class is protected and can’t be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a piece of text that contains another regular expression. Sample text: ...<rege>!^.*$!mailto:asdf@adsfsdaf.com!</rege>...
I'm learning MVVM through a project and I got stuck on something simple. I
I'm trying to create a speech to text app, to get started I got
I've got a sample application on http://github.com/niklassaers/Test-iPhone-TabBar-App that shows my problem: I have a
I was just wondering has anyone got a sample eclipse project with a working
I got some sample code from the net here: http://www.javadb.com/sending-a-post-request-with-parameters-from-a-java-class That works fine. It
I got some file sample.mt from client, and when i open info, it reveals
I've got legacy code sample which uses JFreeChart and XYPlot. Now i'm making i18n
I've got a source code file that started as a copy of some sample
When I compiled the sample codes of C++, I got following info: c++ excxx_example_database_read.cpp

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.