Here is the C# code I have working for this:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace ListboxOfFoobar
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<FooBar> all = (ObservableCollection<FooBar>)FindResource("foobars");
all[0].P1 = all[0].P1 + "1";
}
}
public class FooBar : INotifyPropertyChanged
{
public FooBar(string a1, string a2, string a3, string a4)
{
P1 = a1;
P2 = a2;
P3 = a3;
P4 = a4;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private String p1;
public string P1
{
get { return p1; }
set
{
if (value != this.p1)
{
this.p1 = value;
NotifyPropertyChanged("P1");
}
}
}
private String p2;
public string P2
{
get { return p2; }
set
{
if (value != this.p2)
{
this.p2 = value;
NotifyPropertyChanged("P2");
}
}
}
private String p3;
public string P3
{
get { return p3; }
set
{
if (value != this.p3)
{
this.p3 = value;
NotifyPropertyChanged("P3");
}
}
}
private String p4;
public string P4
{
get { return p4; }
set
{
if (value != this.p4)
{
this.p4 = value;
NotifyPropertyChanged("P4");
}
}
}
public string X
{
get { return "Foooooo"; }
}
}
public class Foos : ObservableCollection<FooBar>
{
public Foos()
{
this.Add(new FooBar("a", "b", "c", "d"));
this.Add(new FooBar("e", "f", "g", "h"));
this.Add(new FooBar("i", "j", "k", "l"));
this.Add(new FooBar("m", "n", "o", "p"));
}
}
}
Here is the XAML:
<Window x:Class="ListboxOfFoobar.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListboxOfFoobar"
xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"
Title="Window1" Height="300" Width="300"
>
<Window.Resources>
<local:Foos x:Key="foobars" />
<DataTemplate x:Key="itemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock MinWidth="80" Text="{Binding Path=P1}"/>
<TextBlock MinWidth="80" Text="{Binding Path=P2}"/>
<TextBlock MinWidth="80" Text="{Binding Path=P3}"/>
<TextBlock MinWidth="80" Text="{Binding Path=P4}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<DockPanel>
<ListBox DockPanel.Dock="Top"
ItemsSource="{StaticResource foobars}"
ItemTemplate="{StaticResource itemTemplate}" Height="229" />
<Button Content="Modify FooBar" Click="Button_Click" DockPanel.Dock="Bottom" />
</DockPanel>
</Window>
Pressing the Button causes the first property of the first FooBar to be updated and for it to show in the ListBox.