开发者

Can't bind to IEnumerable implementation using yield

开发者 https://www.devze.com 2023-01-12 07:57 出处:网络
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 display

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>();
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消