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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:49:51+00:00 2026-05-31T00:49:51+00:00

I have a ComboBox which content might change. The issue is when the content

  • 0

I have a ComboBox which content might change. The issue is when the content is changed, 1) i lose the previous selectedIndex, and 2) SelectedIndex is set to -1.
I would like an MVVM solution, not some solution involving code-behind.
I can have the SelectedIndex set to 0 on content change, but -1 is not a valid value for my model.

What i did as a ‘trick’ was to have the setter of the property bound to SelectedIndex to set the value to 0 when the value is set to -1. So now i don’t have incorrect value in my model. But the ComboBox does not display the “0”th item, rather an empty box.

What could i do to have my ComboBox display the first item (index 0) when its content changes ?

  <ComboBox Name="WeeksComboBox"      IsEditable="False"
                                      ItemsSource="{Binding Weeks}"
                                      SelectedIndex="{Binding WeekNumber}"
                                      >

Weeks is a Notifying property that is a list of strings. It is changed when i change the Year property. WeekNumber is a notifying property of type integer. Now when it is set to -1, i set the corresponding private member to 0 to avoid wrong value in my model.

  • 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-31T00:49:53+00:00Added an answer on May 31, 2026 at 12:49 am

    I always do that in the code-behind when I change the source

    For example,

    Weeks = GetNewWeeksCollection();
    WeekNumber = 0;
    

    An alternative is to use the PropertyChange notification

    void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "Weeks")
            WeekNumber = 0;
    }
    

    Of course, that only works if you actually set Weeks to a new collection. If you simply clear it and reset it, you’ll have to use the CollectionChanged event

    void Weeks_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Reset)
            WeekNumber = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage in which I have a div that I would like
I have got ComboBox which is populated with collection of customTypes. On comboBox change,
i have a combobox which is bound to a datatable column like this: ComboBox.DataContext
i have a combobox which is displaying date from database...in my database the date
I have a combobox from which i need to programmatically disable items depending on
I have a ComboBox in WPF which is databound, and has data template which
I have a datagridview and a combobox which get populated randomly with data. However,
I have a simple UserControl containing a ComboBox which is empty at first. The
I have one combobox on the window form and I have one method which
Say I have a DataGridView in which I dynamically build up a ComboBox column

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.