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;