R – silverlight 3 collection binding

bindingcollectionssilverlight

Someone please help me understand why this binding does not work…

I have a class called SelectionManager with a property called 'dates' which is populated by a WCF service. The property is an array of structs which bundles a DateTime and an integer count of business objects.

public class SelectionManager : INotifyPropertyChanged {

    ... other properties ...

    public DQMServiceDateCountPair[] dates { get; private set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName) {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    }

I have another class called DateSelector which has a DependencyProperty called 'pairs' setup to be the binding target of 'dates'.

public partial class DateSelector : UserControl {

... other stuff ...

public static readonly DependencyProperty pairsProperty = DependencyProperty.Register(
    "pairs",
    typeof(DQMServiceDateCountPair[]),
    typeof(DateSelector),
    new PropertyMetadata(new DQMServiceDateCountPair[0])
);

public DQMServiceDateCountPair[] pairs {
    get { return (DQMServiceDateCountPair[])GetValue(pairsProperty); }
    set { 
        Debug.WriteLine("adding dates");
        SetValue(pairsProperty, value);
        dateMode = DateMode.Years;
    }
}

}

In my MainPage.xaml, I have a line like this:

<date:DateSelector x:Name="dateSelector" pairs="{Binding dates}" />

It's weird, because all my other bindings in MainPage.xaml update correctly, including a ComboBox bound to 'dates'. My UserControl however, will not update. The Debug.Writeline doesn't get called in the set statement of the 'pairs' property.

In playing around with it, I've tried making the DQMServiceDateCountPair[] property into an ObservableCollection and implementing INotifyCollectionChanged, but that doesn't help.

If I leave either the source property or the target property as an array, and make the other an ObservableCollection, then I get a binding error that says it can't automatically convert one to the other, so Silverlight seems aware of the binding, it just doesn't update it.

Can anyone help?

P.S. I'm using Silverlight 3.

Best Answer

Try changing your code as follows:
1. Add DataMember/DataContract attributes
2. Make "set" public

[DataContract]
public class SelectionManager : INotifyPropertyChanged {

    [DataMember]
    public DQMServiceDateCountPair[] dates { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName) {
        if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); }
    }
Related Topic