开发者

PagedCollectionView memory leak Silverlight 4

开发者 https://www.devze.com 2023-01-12 17:26 出处:网络
I have observable collection with some items. /// <summary> /// The <see cref=\"Items\" /> property\'s name.

I have observable collection with some items.

 /// <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);
        }
    }

and also pagedCollectionView because I have to group items in datagrid

 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

in viewmodel const开发者_C百科ructor I set

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

and in view a have datagrid that 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>

when i change items in Items (clear and add new) after many times I have a memory leak.. When I remove ItemsSource everything is fine.. So I know that PagedCollectionView causing memory leak but i don't know why. Any idea, please? Or another solution to group items inside datagrid by some property in collection.. Thank you!!


The issue is the paged collection view hooked up the the NotifyPropertyChanged from your RaisePropertyChanged(ItemsPropertyName); and never releases the event hook... I solved this because I did not need grouping by returning an ICollectionView. When you bind the grid to an ObservableCollection the datagrid will create the PagedCollectionView for you. When you bind to ICollectionView the grid will use the ICollectionView without creating the PagedCollectionView. Hope this helps...

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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号