none
ListCollectionView showing wrong number of items

    Question

  • Hi,

    I have a DataGrid in one of my projects and I have it Bound to a ListCollectionView.  The ListCollectionView has an ObservableCollectioin as it's source collection.  My setup is like the below...

    private readonly ObservableCollection<CountryView> _countryRows = new ObservableCollection<CountryView>();
            private readonly ListCollectionView _countryRowsView;
    public Constructor()
    {
        _countryRowsView = new ListCollectionView(_countryRows);
    }


    I then insert items in to the source collection using a method...

    public void SetupCountries()
    {
        lock (_syncLock)
        {
            if (_countryRows.Count != _countries.Count)
            {
                _countryRows.Clear();
                _countryRowsView.Refresh();
                foreach (string country in _countries)
                {
                    _countryRows.Add(GetCountryData(country));
                    _countryRowsView.Refresh();
                }
            }
        }
    }

    However, whenever I get to the SetupCountries method (even for the first time) my source collection _countryRows will have zero items (Count = 0) but my ListCollectionView _countryRowsView.Count will always be 1 even after doing a _countryRows.Clear and refreshing the ListCollectionView.  This results in an extra blank row being added to my DataGrid.

    Can you see what I might be doing wrong here?  I have checked and there is only 1 real row being added to the _countryRows collection but the ListCollectionView always has a count of 2.

    Any idea?

    Many thanks

    Friday, April 13, 2012 3:38 PM

Answers

  • I think I have found the issue.  In my DataGrid definition I set the Style equal to a style I have defined in a resource file.  One of the definitions in here is that CanUserAddRows="False" amongst other definitions.  This (and only this) attribute wasn't picked up so once I added "CanUserAddRows="False" again in the <DataGrid..> tag the extra row no longer showed.

    I can't understand why this happeed for this one particualar DataGrid as I have other DataGrid's referring to the same style definition and they didn't have the same problem.

    Anyway, that is how I fixed it.  Thanks for your time on this.

    Tuesday, April 17, 2012 7:55 AM

All replies

  • Hi wallaceoc,

    Refer to below code snippet to define your "ListCollectionView", instead of in "Constructor()"

    private ObservableCollection<CountryView> _countryRows;
    private ListCollectionView _countryRowsView;
    public ListCollectionView CountryRowsView
            {
                get
                {
                    if (_countryRowsView== null)
                    {
                        _countryRowsView= new ListCollectionView(_countryRows);
                    }
                    return _countryRowsView;
                }
            }

    For more information about how to complete it, refer to this article:

    http://www.codeproject.com/Articles/47914/Working-with-ObservableCollection-T

    there is a detail explaination included.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 16, 2012 7:22 AM
    Moderator
  • Hi Sheldon,

    thanks for your response... however, it does not seem to fix my problem.

    I now only create the ListCollectionView when it is first referenced.  This appears to be when the Binding is first referenced when the Page is loaded.  At this point the count of the ListCollectionView and the number of rows in the source collection are both zero.

    However, when the page gains focus it calls a method SetupCountries() as detailed in my first post.  However, at the entry point to this method even if the source collection has a zero count the ListCollectionView still has a count of 1.  I don't reference it anywhere else so I cannot see where the empty row is getting added to the ListCollectionView.

    Any ideas?

    Thanks

    Monday, April 16, 2012 10:58 AM
  • Hi wallaceoc,

    Could you share a simple sample to repro this issue with me?

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 17, 2012 5:51 AM
    Moderator
  • I think I have found the issue.  In my DataGrid definition I set the Style equal to a style I have defined in a resource file.  One of the definitions in here is that CanUserAddRows="False" amongst other definitions.  This (and only this) attribute wasn't picked up so once I added "CanUserAddRows="False" again in the <DataGrid..> tag the extra row no longer showed.

    I can't understand why this happeed for this one particualar DataGrid as I have other DataGrid's referring to the same style definition and they didn't have the same problem.

    Anyway, that is how I fixed it.  Thanks for your time on this.

    Tuesday, April 17, 2012 7:55 AM