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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:13:51+00:00 2026-06-10T19:13:51+00:00

May be it’s a silly (or more than trivial) kinda question, but it seems

  • 0

May be it’s a silly (or more than trivial) kinda question, but it seems i just don’t know the answer. Here’s the case –

  1. I assigned a UserList as the ItemsSource of a combobox. So what i did essentially is assigning a reference type to another.
  2. I cleared the UserList. So now i get the Count of the ItemsSource 0 as well.
  3. I still get the items present in my combobox. And i also can cast the SelectedItem of the combobox to a User object.

Here’s the complete code –

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public partial class MainWindow : Window
{
    private List<User> _userList;

    public MainWindow()
    {
        InitializeComponent();
        _userList = new List<User>()
                                  {
                                      new User() {Id = 1, Name = "X"},
                                      new User() {Id = 2, Name = "Y"},
                                      new User() {Id = 3, Name = "Z"}
                                  };
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.comboBox1.ItemsSource = _userList;
        this.comboBox1.DisplayMemberPath = "Name";
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        _userList.Clear();

        /* ItemsSource is cleared as well*/
        IEnumerable userList = this.comboBox1.ItemsSource;

        /*I can still get my User*/
        User user = this.comboBox1.SelectedItem as User;
    }
}  

So, where the items are coming from? What actually happens under-the-hood when i make such binding? Does the control have some kind of cache? It’s a royal pain to realize not having such basic ideas. Can anybody explain the behind-the-scene detail?

EDIT : I wrote the code in WPF, but i have the same question for WinForms Combobox.

EDIT : Doesn’t a combobox display its items from it’s in-memory Datasource? When that datasource contains 0 items, how does it display the items?

  • 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-10T19:13:52+00:00Added an answer on June 10, 2026 at 7:13 pm

    When you set an ItemsSource of any ItemsControl it copies the ref to the list into its Items property. Then it subscribes to the OnCollectionChanged event, and creates a CollectionView object. So, on the screen you can see that collectionView.

    as I have found in source code ItemCollection holds two lists:

    internal void SetItemsSource(IEnumerable value)
        {
          //checks are missed
          this._itemsSource = value;
          this.SetCollectionView(CollectionViewSource.GetDefaultCollectionView((object) this._itemsSource, this.ModelParent));
        }
    

    How could you get SelectedItem?

    This is my assumption from quick look into the source code:

    ItemsControl has a collection of “views” and each View sholud store a ref to the item (User instance), because it has to draw data on the screen. So, when you call SelectedItem it returns a saved ref.

    Upd about references

    Assume there is an User instance. It has the adress 123 in memory. There is a list. It stores references. One of them is 123.

    When you set an ItemsSource ItemsControl saves a reference to the list, and creates a Views collection. Each view stores a references to an item. One view stores an address 123.

    Then you cleared a list of users. Now list doesn’t contains any references to Users. But in memory there is an adrress 123 and there is an instance of User by this adress. Garbage Collector doesn’t destroy it, because View has a reference to it.

    When you get SelectedItem it returns User instance from the 123 adress.

    var user = new User();
    
    var list = new List<User>();
    list.Add(user);
    
    list.Clear();
    Console.WriteLine(list.Count()); //prints 0 - list is empty
    
    Console.WriteLine(user == null); //prints false. - user instance is sill exists;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

May be My Question is not seems like a good one but here is
May be this question seems to be stupid, but strangely I find it hard
May be a silly question, but what's a class called that inherits from NSManagedObject?
May be this question is too easy or I did not know how easy
May be a simple question, but if I have a ModelForm: class ExampleModelForm(forms.ModelForm): class
May sound like a total noob question but i'm looking for a javascript (and
May sound like a stupid question, but I simply can't find the function of
May be this question has been answered before but I couldn't find it. I
May be it's a silly question. Is there an alternative to Java and .NET
May be this question a bit not for StackOverflow, but both compilers and Verilog

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.