views:

230

answers:

2

Here's a basic example to explain my problem. Let's say I have

ObservableCollection<int> Numbers {get; set;}

and an IValueConverter that returns the sum of Numbers.

Normally what I'd do is changed the IValueConverter into an IMultiValueConverter and bind a second value to Numbers.Count like this

<MultiBinding Converter="{StaticResource SumTheIntegersConverter}">
    <Binding Path="Numbers"  />
    <Binding Path="Numbers.Count" />
</MultiBinding>

However I'm unable to use this method to solve my actual problem. It seems like there should be a better way to update the binding when the collection changes that I'm just not thinking of. What's the best way to get the value converter to run when items are added and removed to Numbers?

A: 

This is actually surprisingly very difficult. An IValueConverter doesn't update, so this does not work as you'd hope.

I wrote a sample on the Microsoft Expression Gallery called Collection Aggregator that shows a working, if convoluted, approach to making this work via a Behavior that does the aggregation (Count, in your case, although I also support Sum, Average, etc) for you, instead of a converter.

Reed Copsey
That wouldn't be a bad solution for my example but unfortunately my real case is more complicated. In the real case I'm binding to an ObservableDictionary and using an IMultiValueConverter to index into the dictionary. Your solution wouldn't extend well to my case. The example was just the easiest way I could think of to demonstrate the root of the problem I'm having.
Bryan Anderson
It could be adapted for that scenario - unfortunately, the converter approach doesn't provide a way to have updates (unless you change the classes inside your dictionary, as well).
Reed Copsey
A: 

I ended up doing something like this which seems to work. It's far from an optimal solution and I'd still be interested in something better but it seems to work for my purposes.

class CollectionChangedHandlingValueConverter : IValueConverter
{
    DependencyObject myTarget;
    DependencyProperty myTargetProperty;

    //If this ever needs to be called from XAML you can make it a MarkupExtension and use ProvideValue to set up the Target and TargetProperty
    public CollectionChangedHandlingValueConverter(DependencyObject target, DependencyProperty dp)
    {
     myTarget = target;
     myTargetProperty = dp;
    }

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     INotifyCollectionChanged collection = value as INotifyCollectionChanged;
     if (collection != null)
     {
      //It notifies of collection changed, try again when it changes
      collection.CollectionChanged += DataCollectionChanged;
     }

     //Do whatever conversions here
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
     throw new NotImplementedException();
    }

    #endregion

    void DataCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
     if ((myTarget != null) && (myTargetProperty != null))
     {
      BindingOperations.GetBindingExpressionBase(myTarget, myTargetProperty).UpdateTarget();
     }
    }
}
Bryan Anderson