Looks like this is caused by the converter. ObservableCollection implements INotifyCollectionChanged, which notifies the UI when there is a change to the collection (Add/Remove/Replace/Move/Reset). These are all changes to the collection, not the contents of the collection, and so the updates you were seeing before were due to your class implementing INotifyPropertyChanged. Because the MultiCoverter is returning a new collection of new objects, the data in the initial collections will not propagate to these, since there's no bindings to the original objects for them to notify.
The first thing I would suggest is to take a look at the CompositeCollection Element and see if that will fit your needs.
Instead of setting the ItemsSource as you are, you could maintain the original objects with something like:
<ctrls:DataGrid.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Targets}" />
<CollectionContainer Collection="{Binding Actuals}" />
</CompositeCollection>
</ctrls:DataGrid.ItemsSource>
(I'm assuming 'does not respond to any changes in the underlying data' refers to changing the values, not modifying the collection, if I'm incorrect let me know and I'll take a deeper look at it.)
Edit Additions
In the case that that doesn't work an alternative is to write a new class that will wrap both the Target and Actual collections. Then a single ObservableCollection can be created using these wrappers. This is actually a better method over using a ValueConverter or using a CompositeCollection. With either you loose some of the functionality that was originally present. By using a value converter to recreate a collection, it no longer is bound directly to the original objects and so property notification may be lost. By using the CompositeCollection, you no longer have a single collection that can be iterated through or modified with add/delete/move etc, as it has to know which collection to operate upon.
This type of wrapping functionality can be quite useful in WPF, and is a very simplified version of a ViewModel, a part of the M-V-VM design pattern. It can be used when you don't have access to the underlying classes to add INotifyPropertyChanged or IDataErrorInfo, and can also help add additional functionality such as state and interaction to the underlying models.
Here's a short example demonstrating this functionality where both of our initial classes have the same Name property and don't implement INotifyPropertyChanged that is not shared between them.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Foo foo1 = new Foo { ID = 1, Name = "Foo1" };
Foo foo3 = new Foo { ID = 3, Name = "Foo3" };
Foo foo5 = new Foo { ID = 5, Name = "Foo5" };
Bar bar1 = new Bar { ID = 1, Name = "Bar1" };
Bar bar2 = new Bar { ID = 2, Name = "Bar2" };
Bar bar4 = new Bar { ID = 4, Name = "Bar4" };
ObservableCollection<FooBarViewModel> fooBar = new ObservableCollection<FooBarViewModel>();
fooBar.Add(new FooBarViewModel(foo1, bar1));
fooBar.Add(new FooBarViewModel(bar2));
fooBar.Add(new FooBarViewModel(foo3));
fooBar.Add(new FooBarViewModel(bar4));
fooBar.Add(new FooBarViewModel(foo5));
this.DataContext = fooBar;
}
}
public class Foo
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Bar
{
public int ID { get; set; }
public string Name { get; set; }
}
public class FooBarViewModel : INotifyPropertyChanged
{
public Foo WrappedFoo { get; private set; }
public Bar WrappedBar { get; private set; }
public int ID
{
get
{
if (WrappedFoo != null)
{ return WrappedFoo.ID; }
else if (WrappedBar != null)
{ return WrappedBar.ID; }
else
{ return -1; }
}
set
{
if (WrappedFoo != null)
{ WrappedFoo.ID = value; }
if (WrappedBar != null)
{ WrappedBar.ID = value; }
this.NotifyPropertyChanged("ID");
}
}
public string BarName
{
get
{
return WrappedBar.Name;
}
set
{
WrappedBar.Name = value;
this.NotifyPropertyChanged("BarName");
}
}
public string FooName
{
get
{
return WrappedFoo.Name;
}
set
{
WrappedFoo.Name = value;
this.NotifyPropertyChanged("FooName");
}
}
public FooBarViewModel(Foo foo)
: this(foo, null) { }
public FooBarViewModel(Bar bar)
: this(null, bar) { }
public FooBarViewModel(Foo foo, Bar bar)
{
WrappedFoo = foo;
WrappedBar = bar;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
And then in the Window:
<ListView ItemsSource="{Binding}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
<GridViewColumn Header="Foo Name" DisplayMemberBinding="{Binding FooName}"/>
<GridViewColumn Header="Bar Name" DisplayMemberBinding="{Binding BarName}"/>
</GridView>
</ListView.View>
</ListView>
Brute force:
- Attach handler to each PropertyChanged event for each child item
- Grab the ListCollectionView from your CollectionViewSource
- Call Refresh.
EDIT:
The code for 1, 2 would live in your code-behind.
For #1, you'd do something like:
private void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach( SomeItem item in e.NewItems)
{
item.PropertyChanged += new PropertyChangedEventHandler(_SomeItem_PropertyChanged);
}
break;
....
**HANDLE OTHER CASES HERE**
....
}
}
For #2, in your CollectionChanged handler, you would do something like:
private void _SomeItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(theListBox.ItemsSource));
lcv.Refresh();
}
EDIT2:
However, in this case, I would strongly suggest that you also check ListCollectionView.NeedsRefresh and only refresh if that is set. There's no reason to re-sort if your properties have changed which don't affect the sort.
Best Answer
Note 1
In a nutshell - you should subscribe to INotifyPropertyChanged.PropertyChanged event of all your "scores", and in the event handler you should recalc your TotalScore. But the problem is that you should also monitor changes in your Scores collection (if you need it, of course).
As a very useful helper you can try to use my Extended ObservableCollection. This class already contains ItemPropertyChangedEvent, so you should just write something like:
Note 2
Take a look also at this article. It will teach how to avoid "magic strings" in notifications (like
RaisePropertyChanged("Scores")
ande.PropertyName == "Score"
).Note 3
Instead of
it's better to use
But this is not necessarily, this is just my style of coding :)
BTW, why it's mixed - "average" and "sum"? Is this mistake?