C# 绑定到引用保持不变的IEnumerable时,将忽略PropertyChanged

C# 绑定到引用保持不变的IEnumerable时,将忽略PropertyChanged,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我创建了一个简单的示例来说明绑定问题。 IEnumerable NewReference按预期更新。 IEnumerable SamereReference不会更新,可能是因为引用相同升起(“相同参考”)不足以使WPF更新引用 即使WPF框架具有相同的引用,是否可以采取措施使其重新评估samerereference xaml: xaml.cs: using System.Collections.Generic; using System.Collections.ObjectModel; us

我创建了一个简单的示例来说明绑定问题。
IEnumerable NewReference
按预期更新。
IEnumerable SamereReference
不会更新,可能是因为引用相同<代码>升起(“相同参考”)不足以使WPF更新引用

即使WPF框架具有相同的引用,是否可以采取措施使其重新评估
samerereference

xaml:


xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Stackoverflow
{
    public partial class MainWindow : Window , INotifyPropertyChanged
    {
        public List<string> data = new List<string> {  };
        public IEnumerable<string> SameReference { get { return data; } } //this returns a reference to an unchanged object
        public IEnumerable<string> NewReference { get { return new List<string>(data); } } //this returns a reference to a new object
        //ObservableCollection<string> conventional is known but not the point of this question

        public event PropertyChangedEventHandler PropertyChanged;

        private void Raise(string propertyName)
        {
            if(null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            data.Add("This is data.");
            Raise("SameReference"); //successful notify, ignored values
            Raise("NewReference"); //successful notify, processed values
        }
    }
}
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows;
使用System.Windows.Controls;
命名空间堆栈溢出
{
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共列表数据=新列表{};
public IEnumerable samerereference{get{return data;}}}//返回对未更改对象的引用
public IEnumerable NewReference{get{return new List(data);}}//返回对新对象的引用
//我们知道可观察到的常规收集,但不是这个问题的重点
公共事件属性更改事件处理程序属性更改;
私有void提升(字符串propertyName)
{
如果(null!=属性更改)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共主窗口()
{
this.DataContext=this;
初始化组件();
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
添加(“这是数据”);
Raise(“SameReference”);//成功通知,忽略值
Raise(“NewReference”);//成功通知,已处理值
}
}
}

这是故意的行为。如果您希望集合更新实现(或使用已经更新的类,如)。

@uuddrlrss-wow-这已经4年了:),是的,它确实更新了。我也将投票结束这个问题。嘿,干杯!这就留下了一个面包屑,这使得这个问题的未来研究人员(像今天的我一样)更加容易
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace Stackoverflow
{
    public partial class MainWindow : Window , INotifyPropertyChanged
    {
        public List<string> data = new List<string> {  };
        public IEnumerable<string> SameReference { get { return data; } } //this returns a reference to an unchanged object
        public IEnumerable<string> NewReference { get { return new List<string>(data); } } //this returns a reference to a new object
        //ObservableCollection<string> conventional is known but not the point of this question

        public event PropertyChangedEventHandler PropertyChanged;

        private void Raise(string propertyName)
        {
            if(null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public MainWindow()
        {
            this.DataContext = this;
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            data.Add("This is data.");
            Raise("SameReference"); //successful notify, ignored values
            Raise("NewReference"); //successful notify, processed values
        }
    }
}