Question

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?

Was it helpful?

Solution

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;

OTHER TIPS

In answer to your comment to @GazTheDestroyer ("... why it doesn't get cleared, and how it holds the items?")

In WPF, when you set the ItemsSource property of an ItemsControl, the control will wrap the list of items in a CollectionView, which is a collection type optimised for use by the UI framework. This CollectionView is assigned to the Items property of the control and is what the display-drawing code actually works from. As you see, this collection is entirely separate of the object you originally assigned to ItemsSource, and so there is no propogation of changes from one to the other. This is why the items are still in the control when you clear the original list: the control is ignoring the original list, and has its own list that contains your objects.

It's for this reason that an ItemsSource value needs to raise events - specifically INotifyCollectionChanged.NotifyCollectionChanged - so that the control knows to refresh the Items list. ObservableCollection implements this interface and raises the correct event, and so the functionality works as expected.

It's hugely important to note that this is nothing like what happens in WinForms, which is why I've been pressing you for the clarification.

EDIT: To clarify, there is no "deep copy." The code that is happening is similar in principle to the following:

private List<object> myCopy;

public void SetItemsSource(List<object> yourCopy)
{
     myCopy = new List<object>();
     foreach (var o in yourCopy)
     {
         myCopy.Add(o);
     }
}

Once this code has run, there's only one copy of every item in your list. But each of the items is in both of the lists. If you change, clear or otherwise manipulate yourCopy, myCopy knows nothing about it. You cannot "destroy" any of the objects that are within the list my clearing yourCopy - all you do is release your own reference to them.

Assuming you are using WPF:

List<User> doesn't fire any event that the UI will recognise to refresh itself. If you use ObservableCollection<User> instead, your code will work.

The key difference is that ObservableCollection implements INotifyCollectionChanged, which allows the UI to recognise that the content of the collection has changed, and thus refresh the content of the ComboBox.

(Note that this does not work in WinForms. In WinForms you can set the DataSource property of the control, but the same ObservableCollection trick does not work here.)

When you set a collection reference to ItemsControl, all the combo gets is a reference, that it knows is enumerable.

It will enumerate the reference and display the items. Whether it does a deep copy or shallow copy is irrelevant, all it has is a reference (memory address effectively).

If you change your collection in some way, the combo has no way of knowing unless you tell it somehow. The reference (address) hasn't changed, everything looks the same to the combo. You seem to be thinking that the object is somehow "live" and the combo can watch the memory changing or something? This isn't the case. All it has is a reference that it can enumerate over. The contents can change but without some trigger the combo doesn't know that, and so will sit doing nothing.

ObservableCollection is designed to overcome this. It implements INotifyCollectionChanged that fires events when it changes, so the Combo knows that it must update its display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top