2016-05-17 26 views
0

在WPF應用程序中,我使用MVVM Light和Fody。在我的視圖模型中,我有一本字典public Dictionary<string, string> OtherParticipants { get; set; }。在我的XAML有一行:WPF提高屬性更改事件的字典

<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"/> 

在我看來模型我調用一個方法:

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 + ")"; 
     } 
    } 
} 

方法體是不是真的相關,其背後的想法是會改變的內容OtherParticipants字典。問題是這個改變沒有反映在我的ListBox中。任何想法都歡迎。
附註:我使用Fody所以我所有的虛擬機都標有[ImplementPropertyChanged]屬性。
編輯:當前實現ObservableDictionary的是在評論鏈接不起作用

回答

0

字典實現可能需要籌集使用Binding.IndexerName作爲其屬性名稱的屬性更改事件。