tags:

views:

232

answers:

4

If I want to bind a collection to a some form of listing control in Silverlight. Is the only way to do it so make the underlying objects in the collection implement INotifyPropertyChanged and for the collection to be an Observablecollection?

If I was using some sort of third party object, for example that returned by a web service, I would have to wrap it or map it to something that implements INotifyPropertyChanged ?

+1  A: 

You can bind a list to any IEnumerable collection or a simple control to any object property. The downside is that you get no chnage notification if items are added to the list or properties are changed. So it depends on your application if this is a problem or not.

Maurice
+5  A: 

No, once you add a service reference to your silverlight project in Visual Studio, you can right click it and configure it such that it uses an ObservableCollection (which is the default setting anyway)

Also, the Service Reference will by default ensure that the service's returned types already implement INotifyPropertyChanged.

Hovito
+1  A: 

As Maurice has said, you can bind to any collection (even IEnumerable) and the binding will work, but you won't get change notifications. Note, however, that you don't need to use ObservableCollection, anything that implements INotifyCollectionChanged will work (although ObservableCollection is the simplest one).

It is not necessary for objects within the collection to implement INotifyPropertyChanged, but if they do, then you'll get notifications on every individual change.

Santiago Palladino
A: 

Just to be clear, you can OneTime bind to any object. If you want to OneWay or TwoWay bind you will ned an object supports one of those interfaces. As mentioned, creating the Service Reference does this for you for objects delivered via webservice. However, if for some reason, you still need to produce a Bindable object from a legacy class, you could implement a Converter that implements IValueConverter and then use it to "wrap" your legacy object in a Bindable one live this:

<UserControl>
    <UserControl.Resources>
        <local:FooToBindableFooConverter x:Key="BindableFooConverter"/>
    </UserControl.Resources>
    <TextBlock Text="{Binding FooInstance, Converter={StaticResource BindableFooConverter}}"/>

</UserControl>

Converters are very powerful and can solve lots of "I need X but I have Y" problems.

caryden