2010-08-24 65 views
1

我有可觀察的收集與一些項目。PagedCollectionView內存泄漏Silverlight 4

/// <summary> 
    /// The <see cref="Items" /> property's name. 
    /// </summary> 
    public const string ItemsPropertyName = "Items"; 

    private ObservableCollection<SomeItem> _items = new ObservableCollection<BrandItem>(); 

    /// <summary> 
    /// Gets the Items property. 
    /// </summary> 
    public ObservableCollection<SomeItem> Items 
    { 
     get 
     { 

      return _items; 
     } 

     set 
     { 
      if (_items == value) 
      { 
       return; 
      } 

      _items = value; 

      // Update bindings, no broadcast 
      RaisePropertyChanged(ItemsPropertyName); 
     } 
    } 

也pagedCollectionView,因爲我有項目組在視圖模型構造數據網格

public const string ItemGroupedPropertyName = "ItemGrouped"; 

    private PagedCollectionView _itemsGrouped; 

    /// <summary> 
    /// Gets the ItemSpecificationsGrouped property. 
    /// </summary> 
    public PagedCollectionView ItemSpecificationsGrouped 
    { 
     get { return _itemsGrouped; } 

     set 
     { 
      if (_itemsGrouped == value) 
      { 
       return; 
      } 

      _itemsGrouped = value; 

      // Update bindings, no broadcast 
      RaisePropertyChanged(ItemGroupedPropertyName); 
     } 
    } 
    #endregion 

我設置

ItemGrouped = new PagedCollectionView(Items); 
ItemGrouped.GroupDescriptions.Add(new PropertyGroupDescription("GroupName")); 

,並在視圖中有DataGrid設置綁定ItemsGrouped

<data:DataGrid ItemsSource="{Binding ItemsGrouped}" AutoGenerateColumns="False"> 
            <data:DataGrid.Columns > 
             <data:DataGridTextColumn IsReadOnly="True" 
              Binding="{Binding ItemAttribut1}" Width="*"/> 
             <data:DataGridTextColumn IsReadOnly="True" 
              Binding="{Binding Attribute2}" Width="*" /> 
            </data:DataGrid.Columns> 

          </data:DataGrid> 

當我改變項目中的項目(清除並添加新的)多次後我有內存泄漏..當我刪除I​​temsSource一切都很好..所以我知道PagedCollectionView導致內存泄漏,但我不知道爲什麼。有什麼想法嗎?或另一種解決方案,通過集合中的某些屬性對datagrid中的項目進行分組.. ..謝謝!

+0

您應該清除現有的集合並將其重新添加到集合中,而不是每次都使用新的集合。 – Jehof 2012-06-26 11:55:26

回答

1

問題是從你的RaisePropertyChanged(ItemsPropertyName)連接了NotifyPropertyChanged的分頁集合視圖;並從不釋放事件掛鉤...我解決了這個問題,因爲我不需要通過返回ICollectionView來進行分組。當您將網格綁定到ObservableCollection時,datagrid將爲您創建PagedCollectionView。當您綁定到ICollectionView時,網格將使用ICollectionView而不創建PagedCollectionView。希望這有助於...

public ICollectionView UserAdjustments 
    { 
     get { return _userAdjustmentsViewSource.View; } 
    } 

    private void SetCollection(List<UserAdjustment> adjustments) 
    { 
     if(_userAdjustmentsViewSource == null) 
     { 
      _userAdjustmentsViewSource = new CollectionViewSource(); 
     } 
     _userAdjustmentsViewSource.Source = adjustments; 
     RaisePropertyChanged("UserAdjustments"); 
    }