2017-09-06 118 views
0

我知道有很多關於綁定問題的問題。但我還沒有找到任何解決方案 - 我也沒有任何線索我的代碼中有什麼問題。WPF綁定不在DataGrid中更新(與分組)

我有2個類別:

public ObservableCollection<TicketEntry> AllEntries {get; set;} 

public ObservableCollection<TicketEntry> Entries {get; set;} 

Entity有一個屬性IsFavourite

private bool _isFavourite = false; 
public bool IsFavourite 
{ 
    get { return _isFavourite; } 
    set 
    { 
     _isFavourite = value; 
     OnPropertyChanged(); 
     OnPropertyChanged("ImageSource"); 
} 

string ImageSource 
{ 
    get 
    { 
     if (IsFavourite) 
     { 
      return "pack://application:,,,/Resources/star_true.png"; 
     } 
     else 
     { 
      return "pack://application:,,,/Resources/star_false.png"; 
     } 
    } 
} 

<CollectionViewSource x:Key="Collection" 
         Source="{Binding Entries, 
           UpdateSourceTrigger=PropertyChanged, 
           Mode=TwoWay, 
           NotifyOnSourceUpdated=True}" 
         xmlns:s="clr-namespace:System;assembly=mscorlib"> 
     <CollectionViewSource.GroupDescriptions> 
      <PropertyGroupDescription PropertyName="IsFavourite" /> 
     </CollectionViewSource.GroupDescriptions> 
     <CollectionViewSource.LiveGroupingProperties> 
      <s:String>IsFavourite</s:String> 
     </CollectionViewSource.LiveGroupingProperties> 
    </CollectionViewSource> 

//Handler for Command ((un)set-favourite) 
private void HandleInvertFavourite() 
    { 
     if (IsFavourite) 
     { 
      IsFavourite = false; 
     } 
     else 
     { 
      IsFavourite = true; 
     } 
     AllTicketsViewModel.Instance.InvokeFavouritesChanged(this); 
    } 



internal void InvokeFavouritesChanged(TicketEntry ticketEntry) 
{ 
    if(ticketEntry.IsFavourite) // Add 
     { 
      if(CurrentWeekViewModel.Instance.Entries.Any(x => x.TicketId == ticketEntry.TicketId)) // already exists 
      { 
       TicketEntry existingentry = CurrentWeekViewModel.Instance.Entries.First(x => x.TicketId == ticketEntry.TicketId); 
       CurrentWeekViewModel.Instance.Entries.First(x => x.TicketId == ticketEntry.TicketId).IsFavourite = true; 
      } 
      else 
      { 
       CurrentWeekViewModel.Instance.Entries.Add(ticketEntry); 
      } 
     } 
     else // Remove 
     { 
      if(CurrentWeekViewModel.Instance.Entries.Any(x => x.TicketId == timeTicketEntry.TicketId)) 
      { 
       TicketEntry existingentry = CurrentWeekViewModel.Instance.Entries.First(x => x.TicketId == ticketEntry.TicketId); 
       if (existingentry.TimeBookings.Any(x => x.Date > CurrentWeekViewModel.Instance.SelectedWeek.FirstDay && 
                 x.Date < CurrentWeekViewModel.Instance.SelectedWeek.LastDay && 
                 existingentry.IsFavourite)) 
       //has time data for actual week => keep but set fav = false 
       { 

        existingentry.IsFavourite = false; 
       } 
       else // completely remove 
       { 

        CurrentWeekViewModel.Instance.Entries.Remove(existingentry); 
       } 
      } 
     } 
} 

CurrentWeek -DataGird有GroupingIsFavourite - 我希望現有的條目切換組IsFavourite_Changed

事情我已經檢查:

  • 綁定正確
  • INotifyPropertyChanged的正確實施
  • 添加新的收藏作品(僅適用於現有的有問題)

爲什麼ISN」當屬性改變時分組會發生變化?

+0

嘗試將CollectionViewSource的IsLiveGroupingRequested屬性設置爲true。 – mm8

+0

@ mm8你的意思是分組正確^^? –

回答

1

CollectionViewSourceIsLiveGroupingRequested屬性設置爲true