简体   繁体   中英

Why does a string INotifyPropertyChanged property update but not a List<string>?

In the following WPF application, when you click the button, why does TheTitle TextBlock update but FilesCopied ListBox not update?

XAML:

<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>

code-behind:

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
    }
}

Answer:

Thanks Robert, I forgot about ObservableCollection. Here is the answer:

Change the FilesCopied block to this:

#region ViewModelProperty: FilesCopied
private ObservableCollection<string> _filesCopied = new ObservableCollection<string>();
public ObservableCollection<string> FilesCopied
{
    get
    {
        return _filesCopied;
    }

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

And add:

using System.Collections.ObjectModel;

Your change-handler is on the setter of the list; but you aren't calling the setter (changing the list) - you are adding items to the existing list. There are separate interfaces ( IBindingList / IBindingListView etc) for handling list notifications. BindingList<T> is a reasonable default implementation for a 2.0 list with notification support.

In .NET 3.0 and above, see also INotifyCollectionChanged and ObservableCollection<T>

IMO:

    private readonly ObservableCollection<string> filesCopied =
            new ObservableCollection<string>();
    public ObservableCollection<string> FilesCopied {
        get { return filesCopied; }
    }

Because it doesn't implement IBindingList(View) and therefor the UI never knew that you put anything new into the list.

Use a BindingList or ObservableCollection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM