为什么一个IEnumerable< T>要求到了ToList调用更新列表视图? [英] Why does an IEnumerable<T> require a call to ToList to update the listview?

查看:154
本文介绍了为什么一个IEnumerable< T>要求到了ToList调用更新列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定有很好的解释这一点。我猜它是与我有一个寒冷和缺少明显的东西...

I'm sure there is good explanation for this. I'm guessing it has something to do with me having a cold and missing something obvious...

我有一个简单的窗口:

<Window x:Class="WpfIdeas.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:w="clr-namespace:WpfIdeas"
    Title="Window1" Height="300" Width="315">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" x:Name="btnAddObject" Click="btnAddObject_Click">Add Object</Button>
        <ListView Grid.Row="1"  ItemsSource="{Binding Objects}">
        </ListView>
    </Grid>
</Window>

窗口背后的code是:

the code behind the window is:

using System.Windows;

namespace WpfIdeas
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            DataContext = new ObjectVM();
        }

        private void btnAddObject_Click(object sender, RoutedEventArgs e)
        {
            (DataContext as ObjectVM).AddObject();
        }
    }
}

和它的DataContext设置为以下类:

And its DataContext is set to be the following class:

class ObjectVM : INotifyPropertyChanged
{
    private readonly List<ObjectModel> objects = new List<ObjectModel>();

    //public IEnumerable<ObjectModel> Objects { get { return objects } } //doesn't work
    public IEnumerable<ObjectModel> Objects { get { return objects.ToList() } } //works

    private Random r = new Random();

    public void AddObject()
    {
        ObjectModel o = new ObjectModel(r);
        objects.Add(o);
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("Objects"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

ObjectModel 类,其实,当它被实例化,产生一个14字符串的结构。这是的ToString()方法只输出此字符串。

The ObjectModel class is in fact a struct that generates a 14 character string when it is instantiated. It's ToString() method just outputs this string.

由于code是上面,当我点击添加对象按钮,然后一个新的字符串出现在的ListView

As the code is above, when I click the "Add Object" button then a new string appears in the ListView.

但是,如果我删除了ToList()在属性,不会将任何东西显示在对象调用的ListView 。它只是保持空白。

However if I remove the ToList() call in the Objects property, nothing is ever displayed in the ListView. It just remains blank.

这是为什么?

推荐答案

使用集合对象作为绑定源

您可以枚举实现IEnumerable接口的集合。然而,建立动态绑定,从而使集合中插入或删除自动更新的用户界面,集合必须实现INotifyCollectionChanged接口。此接口公开,必须提出只要底层集合变化的事件。

You can enumerate over any collection that implements the IEnumerable interface. However, to set up dynamic bindings so that insertions or deletions in the collection update the UI automatically, the collection must implement the INotifyCollectionChanged interface. This interface exposes an event that must be raised whenever the underlying collection changes.

这篇关于为什么一个IEnumerable&LT; T&GT;要求到了ToList调用更新列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆