code_junkie

ObservableCollection<T> not updating UI

Nov 12th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. public class MessageList : ObservableCollection<LobbyMessage>
  2. {
  3. public MessageList(): base()
  4. {
  5. Add(new LobbyMessage() { Name = "System", Message = "Welcome!" });
  6. }
  7. }
  8.  
  9. static public MessageList LobbyMessages { get; set; }
  10.  
  11. ChatHelper.LobbyMessages = new MessageList();
  12.  
  13. <ListBox IsSynchronizedWithCurrentItem="True"
  14. ItemsSource="{Binding Mode=OneWay}"
  15. x:Name="ListBoxChatMessages"
  16. d:UseSampleData="True"
  17. ItemTemplate="{DynamicResource MessageListTemplate}"
  18. IsEnabled="True">
  19. <ListBox.DataContext>
  20. <Magrathea_Words_Tools:MessageList/>
  21. </ListBox.DataContext>
  22. </ListBox>
  23.  
  24. Helper.Context = SynchronizationContext.Current;
  25.  
  26. #region IServiceMessageCallback Members
  27.  
  28. /// <summary>
  29. /// Callback handler for when the service has a message for
  30. /// this client
  31. /// </summary>
  32. /// <param name="serviceMessage"></param>
  33. public void OnReceivedServiceMessage(ServiceMessage serviceMessage)
  34. {
  35. // This is being called from the WCF service on it's own thread so
  36. // we have to marshall the call back to this thread.
  37. SendOrPostCallback callback = delegate
  38. {
  39. switch (serviceMessage.MessageType)
  40. {
  41. case MessageType.ChatMessage:
  42. ChatHelper.LobbyMessages.Add(
  43. new LobbyMessage()
  44. {
  45. Name = serviceMessage.OriginatingPlayer.Name,
  46. Message = serviceMessage.Message
  47. });
  48. break;
  49.  
  50. default:
  51. break;
  52. }
  53. };
  54.  
  55. Helper.Context.Post(callback, null);
  56. }
  57.  
  58. ListBoxChatMessages.ItemsSource = ChatHelper.LobbyMessages.Messages;
  59.  
  60. <ListBox IsSynchronizedWithCurrentItem="True"
  61. ItemsSource="{Binding Mode=OneWay}" Background="#FF1F1F1F"
  62. Margin="223,18.084,15.957,67.787" x:Name="ListBoxChatMessages"
  63. ItemTemplate="{DynamicResource MessageListTemplate}"
  64. IsEnabled="True"/>
Add Comment
Please, Sign In to add comment