views:

44

answers:

1

I have a class encapsulating a bunch of collections. I would like to bind this class to a listbox to display the items in the collections. The class implements IEnumerable. When the listbox is displayed I am expecting the IEnumerable.GetEnumerator method to be called. It is however not when the GetEnumerator method uses the yield keyword. However, if I would return the enumerator from a List collection it would work fine, the GetEnumerator method is called each time a window is displayed.

What is so magical about the enumerator of a List collection??? What is the correct interface to implement to allow a WPF itemscontrol to get snapshots (updates not needed)??? Is IList the one to use???

Below sample code adds a timestamp everytime a new window is opened. However, the listbox never shows more than one timestamp, which is the number of timestamps the first (and only time) GetEnumerator is called. Count get increased so timestamps are added. Changing GetEnumerator method to return the enumerator of the list collection will result in that GetEnumerator method gets called each time a new window is opened.

XAML:

<Window x:Class="YieldTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <StackPanel>
        <Button Content="Open" Click="Button_Click" />
        <TextBlock Text="{Binding Path=Count}" />
        <ListBox ItemsSource="{Binding}" />
    </StackPanel>
</Window>

Code behind:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;

namespace YieldTest
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window1 window1 = new Window1();
            window1.DataContext = _timeStamps;
            _timeStamps.Add(DateTime.Now);
            window1.Show();
        }

        private static TimeStamps _timeStamps = new TimeStamps();
    }

    public class TimeStamps : IEnumerable
    {
        public void Add(DateTime time1)
        {
            _timeStamps.Add(time1);
        }

        public int Count { get { return _timeStamps.Count; } }

        public IEnumerator GetEnumerator()
        {
            Debug.WriteLine("GetEnumerator");
            // Returning the enumerator of _timeStamps will result in
            // GetEnumerator called every time a new window is opened,
            // which is the expected result
            // return _timeStamps.GetEnumerator();

            // Using yield will result in GetEnumerator is called only
            // one time for the first window opened. This means that
            // newer windows will have stale data.
            foreach (DateTime timeStamp in _timeStamps)
            {
                yield return timeStamp;
            }
        }

        private List<DateTime> _timeStamps = new List<DateTime>();
    }
}
+1  A: 

When you set the ItemsSource WPF accesses it through a CollectionView to enable sorting, grouping e.t.c. The view is shared so in your case each ItemsControl is using the same Enumerator but it is never reset so only the last item appears.

If you want Snapshotting behaviour there are a few methods but creating a new list for each ItemsControl is the simplest.

If you want all of them to be in sync and update automatically see ObservableCollection which implements INotifyCollectionChanged.

You should probably use the generic IEnumerable interface as well.

Kris
I have also seen that WPF uses collectionviews to get to the data. Why doesn't the enumerator from yield get reset when a enumerator from a list does? What kind of collectionview does it create when you use yield?
Wallstreet Programmer
Found this answer which discuess yield and reset. Thanks for guiding me in the right direction. I guess I will just bite the bullet and implement my own enumerator instead of using yield.http://stackoverflow.com/questions/2308163/ability-to-reset-ienumerator-generated-using-yield-c
Wallstreet Programmer
You can use yield and have the return type be IEnumerable instead of IEnumerator and remove the implementation of IEnumerable. Then set the DataContext to call that method.
Kris