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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:15:29+00:00 2026-05-31T21:15:29+00:00

I have a ComboBox, whose ItemsSource is bound to a new (not default) ListCollectionView,

  • 0

I have a ComboBox, whose ItemsSource is bound to a new (not default) ListCollectionView, which is linked to an ObservableCollection. The ComboBox SelectedItem property is bound to a public SelectedHat property.

Step 1: Select the 2nd item in the ComboBox. SelectedHat is now the 2nd Hat in the list, as expected.
Step 2: (Click the button to) Set the 2nd spot in the list to a new Hat. SelectedHat is first set to null, then set to the new Hat.

Why is SelectedHat set to null before the new Hat?

I want to be able to vm.Collection[index] = new Hat() and
(1) if the ComboBox has that index selected, keep it selected instead of going blank
(2) only set SelectedHat once to the new Hat, instead of null and THEN the new Hat

C#:

public partial class MainWindow : Window
{
    private readonly ViewModel vm;

    public MainWindow()
    {
        InitializeComponent();
        vm = new ViewModel();
        DataContext = vm;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Hat item = new Hat { Name = "hat 2", Color = "Red"};
        vm.Collection[1] = item;
    }
}

public class ViewModel : BaseNotifyPropertyChanged
{
    public ObservableCollection<Hat> Collection { get; set; }
    public ListCollectionView View { get; set; }

    private Hat selectedHat;
    public Hat SelectedHat
    {
        get { return selectedHat; }
        set
        {
            selectedHat = value;
            Console.WriteLine(string.Format("SelectedHat set to [{0}]", value));
            NotifyPropertyChanged("SelectedHat");
        }
    }

    public ViewModel()
    {
        Collection = new ObservableCollection<Hat>()
        {
            new Hat { Name = "hat 1", Color = "Black" },
            new Hat { Name = "hat 2", Color = "Black" },
            new Hat { Name = "hat 3", Color = "Black" },
        };

        View = new ListCollectionView(Collection);
        View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
    }
}

public class Hat
{
    public string Name { get; set; }
    public string Color { get; set; }

    public override string ToString()
    {
        return string.Format("{0} ({1})", Name, Color);
    }
}

public abstract class BaseNotifyPropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML:

<StackPanel>
    <TextBlock Text="{Binding Path=SelectedHat, Mode=OneWay}" />
    <ComboBox ItemsSource="{Binding Path=View}" SelectedItem="{Binding Path=SelectedHat, UpdateSourceTrigger=PropertyChanged}" />
    <Button Content="click me" Click="Button_Click" />
</StackPanel>
  • 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-31T21:15:30+00:00Added an answer on May 31, 2026 at 9:15 pm

    This is the implementation of ObservableCollection.SetItem

    protected override void SetItem(int index, T item)
    {
      this.CheckReentrancy();
      T obj = this[index];
      base.SetItem(index, item);
      this.OnPropertyChanged("Item[]");
      this.OnCollectionChanged(NotifyCollectionChangedAction.Replace, (object) obj, (object) item, index);
    }
    

    as you can see it raises OnPropertyChanged("Item[]") and then OnCollectionChanged(NotifyCollectionChangedAction.Replace, (object) obj, (object) item, index).
    OnCollectionChanged has parameters ‘oldItem’ and ‘newItem’. I expect if we traced the code through to the combo box implementation we would see that the old item was removed and replaced with null, and then the new item was inserted, which is why you get the behaviour your experiencing (I see it as well).

    My work around is instead of replacing the item, add the new item, change the currently selected item and then remove the old item.

    private void ButtonClick(object sender, System.Windows.RoutedEventArgs e)
    {
        Hat newHat = new Hat { Name = "hat 2", Color = "Red" };
    
        var viewModel = (ViewModel)DataContext;
    
        var oldHat = viewModel.Collection[1];
    
        if (viewModel.SelectedHat == oldHat)
        {
            viewModel.Collection.Add(newHat);
            viewModel.SelectedHat = newHat;
            viewModel.Collection.Remove(oldHat);
        }
        else
        {
            viewModel.Collection[1] = newHat;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid whose itemsSource is bound to a multiconverter which uses a
I have a ComboBox whose ItemsSource is bound to an ObjectDataProvider that has its
I have a ComboBox bound to an ObservableCollection of decimals. What is the correct
Scenario : I have a ContentControl in my View whose Content property is bound
I've got a simple question, I've got a ComboBox whose ItemsSource is bound to
I have a ComboBox that doesn't seem to update the SelectedItem/SelectedValue. The ComboBox ItemsSource
I have a ComboBox in WPF whose ItemsSource is set to a list programmatically.
I've got a ComboBox with an ItemsSource which I've bound to a List(Of String).
I have a combobox on my form that is bound to a generic list
I have a ComboBox in WPF which is databound, and has data template which

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.