我有一些项目的可观察集合。
/// <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
在我设置的viewmodel构造函数中
ItemGrouped = new PagedCollectionView(Items);
ItemGrouped.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));
并查看绑定(bind) 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>
当我多次更改项目中的项目(清除并添加新项目)时,我有内存泄漏。当我删除 ItemsSource 时,一切都很好。所以我知道 PagedCollectionView 导致内存泄漏,但我不知道为什么。请问有什么想法吗?或者另一种通过集合中的某些属性对数据网格中的项目进行分组的解决方案。谢谢!
最佳答案
问题是分页 Collection View 从您的 RaisePropertyChanged(ItemsPropertyName); 连接了 NotifyPropertyChanged;并且永远不会释放事件 Hook ......我解决了这个问题,因为我不需要通过返回 ICollectionView 进行分组。当您将网格绑定(bind)到 ObservableCollection 时,数据网格将为您创建 PagedCollectionView。当您绑定(bind)到 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");
}
关于data-binding - PagedCollectionView 内存泄漏 Silverlight 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3557108/