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 Text开发者_运维百科Block 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.
精彩评论