Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Windos
win32.3rdparty
win32.directx.audio
win32.directx.ddk
win32.directx.graphics
win32.directx.input
win32.directx.managed
win32.directx.misc
win32.directx.networking
win32.directx.sdk
win32.directx.video
win32.dirx.grap.shaders
win32.gdi
win32.international
win32.kernel
win32.messaging
win32.mmedia
win32.networks
win32.ole
win32.rtc
win32.tapi
win32.tapi.beta
win32.tools
win32.ui
win32.wince
win32.wmi
windows.mediacenter
winfx.aero
winfx.announcements
winfx.avalon
winfx.collaboration
winfx.fundamentals
winfx.general
winfx.indigo
winfx.sdk
winfx.winfs
  
 
date: Mon, 9 Jun 2008 06:36:37 -0700 (PDT),    group: microsoft.public.windows.developer.winfx.avalon        back       


Binding to INotifyCollectionChanged event   
I am trying to bind a dependency property to an aggregate of a
collection. I've implemented a wrapper around a Dictionary that
implements INotifyCollectionChanged to fire the event when items are
added or removed.

Debugging through it I notice that there is nothing attached to the
event after I bind to it which I expected the SetBinding to do as I
don't know what delegate to bind to the event.

I've tried a test with just an ObservableCollection to see if I was
missing something but that doesn't work either.

How do I wire up the event to the binding to get my test to pass?

Here's my test code:

[TestClass()]
public class ObservableDictionaryTest : DependencyObject
{
    public class MaxDoubleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object
parameter, System.Globalization.CultureInfo culture)
        {
            if (value is IEnumerable<double>)
            {
                return ((IEnumerable<double>)value).Max(); // using
Linq
            }

            throw new ArgumentException("Cannot covert " +
value.GetType() + " to IEnumerable<double>", "value");
        }

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

    public double Max
    {
        get { return (double)GetValue(MaxProperty); }
    }

    public static readonly DependencyProperty MaxProperty =
        DependencyProperty.Register("Max", typeof(double),
typeof(ObservableDictionaryTest), new PropertyMetadata(0.0));

    [TestMethod()]
    public void TestBindingToObservableCollection()
    {
        ObservableCollection<double> values = new
ObservableCollection<double>();
        values.Add(1.0);
        values.Add(2.0);
        values.Add(3.0);

        Binding b = new Binding();
        b.Converter = new MaxDoubleConverter();
        b.Source = values;
        b.Mode = BindingMode.OneWay;
        BindingOperations.SetBinding(this, MaxProperty, b);

        Assert.AreEqual(3.0, Max); // this works

        values.Add(4.0);
        Assert.AreEqual(4.0, Max); // this doesn't

        values.Remove(4.0);
        Assert.AreEqual(3.0, Max);
    }
}
date: Mon, 9 Jun 2008 06:36:37 -0700 (PDT)   author:   Some Bloke

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us