How do I update an IValueConverter on CollectionChanged?

后端 未结 4 1735
天涯浪人
天涯浪人 2020-12-20 22:13

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

ObservableCollection Numbers {get; set;}

and an IValueConverte

相关标签:
4条回答
  • 2020-12-20 22:51

    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.

    0 讨论(0)
  • 2020-12-20 22:51

    And I ended up synchronizing collection (original with converter), take a look at the buttom of my post for example:

    http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

    0 讨论(0)
  • 2020-12-20 23:01

    In your model, subscribe to CollectionChanged and raise PropertyChanged:

    Numbers.CollectionChanged += (o,e) => 
      OnPropertyChanged(new PropertyChangedEventArgs(nameof(Numbers)));
    

    And, as thomasgalliker mentioned, you should unsubscribe from the event when the model containing the connection is no longer used.

    0 讨论(0)
  • 2020-12-20 23:05

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题