C# 为什么字符串INotifyPropertyChanged属性更新而不是列表?

C# 为什么字符串INotifyPropertyChanged属性更新而不是列表?,c#,wpf,generics,data-binding,xaml,C#,Wpf,Generics,Data Binding,Xaml,在下面的WPF应用程序中,当您单击该按钮时,为什么标题文本块会更新,而文件复制列表框不会更新 XAML: 因为它没有实现IBindingListView,因此UI从来不知道您在列表中添加了任何新内容 使用BindingList或ObservableCollection。因为它没有实现IBindingListView,因此UI永远不会知道您在列表中添加了任何新内容 使用BindingList或ObservableCollection。您的更改处理程序位于列表的setter上;但您并不是在调用set

在下面的WPF应用程序中,当您单击该按钮时,为什么标题文本块会更新,而文件复制列表框不会更新

XAML:


因为它没有实现IBindingListView,因此UI从来不知道您在列表中添加了任何新内容


使用BindingList或ObservableCollection。

因为它没有实现IBindingListView,因此UI永远不会知道您在列表中添加了任何新内容


使用BindingList或ObservableCollection。

您的更改处理程序位于列表的setter上;但您并不是在调用setter来更改列表,而是在向现有列表中添加项。有单独的接口IBindingList/IBindingListView等用于处理列表通知。BindingList是具有通知支持的2.0列表的合理默认实现

在.NET 3.0及更高版本中,另请参见INotifyCollectionChanged和

海事组织:


您的更改处理程序位于列表的setter上;但您并不是在调用setter来更改列表,而是在向现有列表中添加项。有单独的接口IBindingList/IBindingListView等用于处理列表通知。BindingList是具有通知支持的2.0列表的合理默认实现

在.NET 3.0及更高版本中,另请参见INotifyCollectionChanged和

海事组织:

<Window x:Class="TestList3433.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <TextBlock Text="{Binding TheTitle}"/>
        <TextBlock Text="above"/>
        <ListBox ItemsSource="{Binding FilesCopied}"/>
        <TextBlock Text="below"/>

        <Button Content="Add to collection" Click="Button_Click"/>
    </StackPanel>
</Window>
using System.Collections.Generic;
using System.Windows;
using System.ComponentModel;

namespace TestList3433
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region ViewModelProperty: FilesCopied
        private List<string> _filesCopied = new List<string>();
        public List<string> FilesCopied
        {
            get
            {
                return _filesCopied;
            }

            set
            {
                _filesCopied = value;
                OnPropertyChanged("FilesCopied");
            }
        }
        #endregion

        #region ViewModelProperty: TheTitle
        private string _theTitle;
        public string TheTitle
        {
            get
            {
                return _theTitle;
            }

            set
            {
                _theTitle = value;
                OnPropertyChanged("TheTitle");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            FilesCopied.Add("test1.txt");
            TheTitle = "This is the title";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FilesCopied.Add("test2.txt");
            TheTitle = "title was changed";
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
#region ViewModelProperty: FilesCopied
private ObservableCollection<string> _filesCopied = new ObservableCollection<string>();
public ObservableCollection<string> FilesCopied
{
    get
    {
        return _filesCopied;
    }

    set
    {
        _filesCopied = value;
        OnPropertyChanged("FilesCopied");
    }
}
#endregion
using System.Collections.ObjectModel;
    private readonly ObservableCollection<string> filesCopied =
            new ObservableCollection<string>();
    public ObservableCollection<string> FilesCopied {
        get { return filesCopied; }
    }