sync object list in wpf chat list if changed

61 views Asked by At

I work with WPF UI of chat application (MVVM model), I can show chat list staic and chats massege static in code but i couldn't sync it when get new data from DB, app works with Singleton

#region Singleton
/// <summary>
/// A single instance of the design model
/// </summary>
/// 
public static ChatListDesignModel Instance => new ChatListDesignModel();

    public ChatListDesignModel()
    {
            Items = new List<ChatListItemViewModel>
            {
                new ChatListItemViewModel
                {
                    Name = "Luke1111111",
                    Initials = "LM",
                    Message = "This chat app is awesome! I bet it will be fast too",
                    ProfilePictureRGB = "3099c5", //fe4503 00d405 3099c5
                    NewContentAvailable = true,
                     IsSelected = true
                },
            };
    }

this item is dispaled but when i add new item to Items its doesn't insert in UI

 <Grid DataContext="{x:Static core:ChatListDesignModel.Instance}" Background="{StaticResource ForegroundLightBrush}">
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ItemsControl   ItemsSource="{Binding Items }">

chat messages defined as ObservableCollection and it also not resresh or take new value from real time DB

1

There are 1 answers

0
Fruchtzwerg On

The view is is not getting informed about changes inside a List. Instead you should use an ObservableCollection. The ObservableCollection

represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

This means your view can recognize changes and updates the GUI like required.

Just change your items to an ObservableCollection<ChatListItemViewModel>. You are also able to initialize it like

Items = new ObservableCollection<ChatListItemViewModel>
{
    ...