In WPF app I'm using MVVM Light and Fody. In my View-model i have a dictionary public Dictionary<string, string> OtherParticipants { get; set; }
. In my XAML there is a line :
<ListBox Height="350" HorizontalAlignment="Right" Margin="0,0,50,15"
Name="participantsListBox" VerticalAlignment="Center"
Width="170" SelectionMode="Single"
ItemsSource="{Binding OtherParticipants}"
SelectedItem="{Binding SelectedParticipant}" DisplayMemberPath="Value"/>
In my View-model I call a method:
private void NewMessagesReceivedHandler(Dictionary<string, List<BaseMessageDto>> result)
{
var targetItems = OtherParticipants.Where(p => result.Keys.Any(n => p.Key == n)).ToList();
var dialogueTarget = SelectedParticipant.Value;
var dictItem = OtherParticipants.FirstOrDefault(i => i.Value == dialogueTarget);
if (targetItems.Contains(dictItem))
{
//there is an open dialogue with this user
//Dialogue.AddRange(result[dialogueTarget]);
result[dialogueTarget].ForEach(m => Dialogue.Add(m));
targetItems.Remove(dictItem);
}
for (int i = 0; i < targetItems.Count(); i++)
{
var participantKey = targetItems[i].Key;
var participantNameWithNum = targetItems[i].Value;
var incomingMessagesCount = result[participantKey].Count;
if (_usernameWithNotificationsCountRegex.IsMatch(participantNameWithNum))
{
var matches = _usernameWithNotificationsCountRegex.Matches(participantNameWithNum)[0];
var realUser = matches.Groups[1].Value;
var currentMessagesCount = Convert.ToInt32(matches.Groups[3].Value);
var total = currentMessagesCount + incomingMessagesCount;
OtherParticipants[participantKey] = realUser + " (" + total + ")";
}
else
{
OtherParticipants[participantKey] = participantNameWithNum + " (" + incomingMessagesCount + ")";
}
}
}
The Method body is not really relevant, the idea behind it that is does change the content of OtherParticipants dictionary. The problem is that this change is not reflected in my ListBox. Any ideas are welcome.
Side note: I'm using Fody so all my VM is marked with [ImplementPropertyChanged]
attribute.
Edit: Current implementation of ObservableDictionary that is linked in comments does not work