ObservableCollectionView Count is always 0.

Sep 16, 2014 at 8:08 PM
var TheNews = new ObservableCollectionView<RNews>(source);
TheNews.Filter = news => news.category == ThisView.Tag.ToString();
var x = TheNews.Count;

x always returns 0.

How can I count the items in the "TheNews" ?

Thanks Much
Coordinator
Sep 16, 2014 at 9:04 PM
Just created the following unit test and Count is as expected:
    public class SampleClass
    {
        public string First { get; set; }
        public string Last { get; set; }
    }

    [TestMethod]
    public void When_filtering_collection_then_count_should_be_correct()
    {
        //// Arrange
        var source = new ObservableCollection<SampleClass>
        {
            new SampleClass {First = "a", Last = "b"},
            new SampleClass {First = "a", Last = "b"},
            new SampleClass {First = "b", Last = "c"},
            new SampleClass {First = "b", Last = "c"},
        };

        //// Act
        var view = new ObservableCollectionView<SampleClass>(source);
        Assert.AreEqual(4, view.Count);
        view.Filter = c => c.First == "a";

        //// Assert
        Assert.AreEqual(2, view.Count);
    }
See https://mytoolkit.codeplex.com/SourceControl/latest#MyToolkit.Tests.Wpf45/Collections/ObservableCollectionViewTests.cs

Are you sure that your filter is correct? Gives Tag.ToString() really the name of the category?

BTW: When tag changes then the collection gets not automatically updated, you have to call Update() on the view...
Coordinator
Sep 17, 2014 at 7:06 AM
Did you find a solution?
Sep 17, 2014 at 2:39 PM
Nope.

I will post the code in full of what I have in a few. I need to put together in a manner that is more clear and exact.
Sep 17, 2014 at 3:16 PM
Edited Sep 17, 2014 at 3:17 PM
rsuter wrote:
Did you find a solution?

Here is a full code sample.

OS TARGET: WP8.1

public class RNews
    {
        public string category { get; set; }
        public string heading { get; set; }
        public string shortDesc { get; set; }
        public string content { get; set; }
        public string image { get; set; }
    }

private ObservableCollection<RNews> source = new ObservableCollection<RNews>();

//***********************************
// I usually have about 80-100 items
//***********************************
RNews NewsCat = new RNews() {
                        category = "Headlines", 
                        heading = "Test Heading", 
                        shortDesc = "This is the short description",
                        content = "This is the item content here......",
                        image = "http://www.server.com/image.png"
                    };

source.Add(NewsCat);

var TheNews = new ObservableCollectionView<RNews>(source); 
TheNews.Filter = news => news.category == "Headlines"; 

//***********************************
// this always displays 0;
//***********************************
var x = TheNews.Count;  

//***********************************
// Works and displays results as expected
// "ThisView" is a GridView
//***********************************
ThisView.ItemsSource = TheNews; 
Thanks.
Coordinator
Sep 17, 2014 at 3:21 PM
Ok, that's strange. I'll check this soon...
My unit tests above are run under WPF maybe this is the problem...
Sep 17, 2014 at 4:02 PM
If you want, I can send in the code in full so you can see what is going on...
Sep 17, 2014 at 4:54 PM
OK ... WEIRD.. Situation ......

I started debugging by commenting out sections and this is what I found out

var requestX = new HttpGetRequest(poiRequest);
var response = await Http.GetAsync(requestX);

//OR

var response = await Http.GetAsync(poiRequest);
var html = response.Response;
usage of the above always returns the count of ObservableCollectionView as 0
string poiRequest = "http://server.com";
            
//Scenario 1
var requestX = new HttpGetRequest(poiRequest);
var response = await Http.GetAsync(requestX);

//Scenario 2
//var responses = await Http.GetAsync(poiRequest);
//var html = responses.Response;

//hardcoding the values for testing....
            RNews NewsCat = new RNews() { category = "a", heading = "b" };
            RNews NewsCat2 = new RNews() { category = "b", heading = "a" };
            RNews NewsCat3 = new RNews() { category = "b", heading = "a" };

            source.Add(NewsCat);
            source.Add(NewsCat2);
            source.Add(NewsCat3);
The Above will return 0 as count on both Scenario 1&2 even though we are not using any returned values. If you comment out both Scenarios, you get proper count returned.


..
Coordinator
Sep 17, 2014 at 5:18 PM
ok, can you send me a sample solution? mail @ rsuter com
Coordinator
Sep 17, 2014 at 5:32 PM
i think ive found the problem... there are some problems with nuget dependencies and binding redirects... are you using the mytoolkit using nuget?
Sep 17, 2014 at 5:57 PM
Yes I am.
Coordinator
Sep 17, 2014 at 8:02 PM
In your sample code, the loaded event is called before the source is initialized...

Have a look at this view model: It also uses an ObservableCollection (AllProjects) and an ObservableCollectionView (FilteredProjects) for the UI.

https://projectdependencybrowser.codeplex.com/SourceControl/latest#ProjectDependencyBrowser/ViewModels/MainWindowModel.cs