EightyOne Unite
EightyOne Unite

Reputation: 11795

WPF databinding, replacing the source object

I have some UI bound to an ObservableCollection of type T where 'T' implements INotifyProperty Changed properly.

The problem is i need to completely swap out the ObservableCollection at runtime since it is popluated from a SQL call. This obviously messes the whole binding up and no change notifications fire.

How can i replace the source object at runtime without losing my binding?

the code is roughly like this

ObservableCollection<Issue> iss = Application.Current.FindResource("Issues") as ObservableCollection<Issue>;
iss = new ObservableCollection<Issue>();
PopulateCollection(iss);

Upvotes: 0

Views: 542

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178660

As long as the property exposing the collection supports change notification (either because it's a dependency property, or because it implements INotifyPropertyChanged), you can just change that property. Anything bound to it will auto-refresh to the new collection.

Upvotes: 4

Related Questions