我有一些可观察的收藏品。
/// <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"));并考虑到绑定ItemsGrouped have datagrid
<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导致了内存泄漏,但我不知道为什么。有什么想法吗?或者通过集合中的某些属性对datagrid中的项进行分组的另一种解决方案。谢谢你!!
发布于 2011-03-18 02:35:53
问题是分页集合视图挂起了来自RaisePropertyChanged的NotifyPropertyChanged (ItemsPropertyName);并且永远不会释放事件挂钩……我解决了这个问题,因为我不需要通过返回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");
}https://stackoverflow.com/questions/3557108
复制相似问题