I want to display all my CONTACTS in a ItemsControl dynamically. I have a List Contacts in my Logic (this one gets updated if someone removed me or if someone accepted my request) and I've added this List to a ObservableCollection<> which is bound to the ListBox.
C#
Contacts = new ObservableCollection<Contact>(MyLogic.Current.Contacts);
XAML
<ItemsControl ItemsSource="{Binding Contacts}" x:Name="MainPanel">
And here's the problem: When I want to add a contact to my Contacts LIST, the ObservableCollection doesn't get updated
MyLogic.Current.Contacts.Add(new Contact("Fred", true));
This is not the best solution but if you want to see where the problem is, the following code updates your UI:
A better solution is when
MyLogic.Current.Contacts
changes notify your UI via events.Edit:
To notify UI when ever your data changes you can use events as follow:
First define an
EventArgs
which shows newly added items like this:Then define an
EventHandler
in yourMyLogic
calss as follow:And finally use your
EventHandler
to notify UI: