![!IMPORTANT] Notice we are using the CollectionChanged event of the Itemssource to save the messages to the database.
private void Items_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems?.Count > 0)
{
foreach (var item in e.NewItems)
{
if (item is SimpleChatItem chatItem)
{
dbContext.Messages.Add(chatItem);
}
}
// After adding all the items to the dbContext, we save the database
dbContext.SaveChanges();
}
}
TODO
TODO