0

I'm building a chat application using WPF and I'm trying to understand which is the best approach for realizing it.
I have a "receive" event in my ViewModel and, when a message arrives, I think to add it to an ObservableCollection<Message> that contains all the messages sent and arrived in chat session.

I should have to open a new chat window when a message arrives from a new user and I have to add the message if it arrives from a user for which the window has already been created.

Is it a good thing to have a unique ObservableCollection for all the messages? If so, how can I inform the view that a message has arrived for a new user? And how can I put a "filter" to the collection for databinding messages for users?

iknow
  • 8,358
  • 12
  • 41
  • 68
Cris
  • 12,124
  • 27
  • 92
  • 159

2 Answers2

1

If you add item to ObservableCollection and your Control in the View (be it grid/listView of anything) should automatically update itself when it is bound to it. But that means you will need to have new chat collection for each new user. Make sure you have handled all the threading issues if you data retrieval is not at GUI Thread.

I would think carefully about how I would design this:

  • Are you interested in persisting/saving your chat history?

  • Can u use other messaging transports like Solace ?

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
1

I think you are on the right track. Here is one approach you can take:

sticking with the MVVM pattern, your model will be listening for new messages. It can maintain a list of current view/viewmodel and user pairs. If a message arrives from a new user, create a new view & its corresponding viewmodel, then just add the messages to the ObservableCollection in the viewmodel. Because the ObservableCollection implements the INotifyCollectionChanged interface, any UI element that you bind to it will automatically get notified when a new item is added to the collection and should then show it.

If you wanted to get really tricky and super efficient, you could have one viewmodel which all the individual views are bound to, and they can just use a filtering mechanism to isolate out the correct messages they should show (hint: use a value converter on the binding to the ObservableCollection). Doing it this way means you only maintain one viewmodel and one ObservableCollection, and it cuts down on the amount of items you need to keep track of.

slugster
  • 49,403
  • 14
  • 95
  • 145
  • Yes, the bad thing is that i would like to create new TabItems inside a TabControl for chat sessions but have this problem: http://stackoverflow.com/questions/6509264/wpf-weird-problem-in-databinding-with-tabcontrol and seems that i cannot use correctly TextBox as content inside a single TabItem so that i don't use databinding for dynamically creating TabItems and i create them in code-behind. So i don't know how to make to VM to inform the View that the ObservableCollection is changed. – Cris Jun 29 '11 at 13:16
  • @Cris - the VM doesn't need to do anything, the ObservableCollection notifies anything boud to it that its collection has changed, no further work required from you. You could also use an [ItemsControl](http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx) inside of a tab page to show your messages. – slugster Jun 29 '11 at 13:29