locked
How to update GridView bound to ObservableCollection without freezing the UI?

    Question

  • Hi, I'm trying to update a GridView that's bound to an ObservableCollection.

    Currently I'm getting the data from a background task. But once I get all the data (more than a 1000 reports) the GridView is updated. But since it can only be done in the UI thread it freezes the application. 

    This is what it roughly looks like. 

            public async Task groupByCatagory()
            {
                ObservableCollection<Accounts> tempCollection = new ObservableCollection<Accounts>();
    
                await Task.Run(() =>
                {
                    //Do suff and get all the accounts in to tempCollection
                });
    
                //Clear all the accounts
                GroupedAccounts.Clear();
    
                //Add the accounts to the GroupedAccounts. 
                foreach (Accounts account in tempCollection)
                {
                    GroupedAccounts.Add(account);
                }
            }

    Is there a way around this? 

    Wednesday, February 12, 2014 12:11 PM

Answers

  • Unfortunately, I'm already using semantic zoom in both of the views. Therefor I won't be able to use it to mimic two different views. 

    But, like you said completely clearing and reloading the collection seems to be a bad idea. Would it be better if I had two differant gridviews of which one is always hidden? 

    This would mean duplication of the data, but it might help when switching between the views. I'll give it a shot tomorrow. Are there any other way I should try out?

    Thank you again for the responses. 

    Wednesday, February 12, 2014 8:20 PM
  • I think this approach is likely the best way to go about this - there's usually a tradeoff between resources and performance. Unless this causes specific issues, this seems like a good way to get this done.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 13, 2014 1:15 PM
    Moderator

All replies

  • It seems a bad idea to completely clear the entire OC and rebind it when you have that much data. What exactly are you doing that requires this process?   


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, February 12, 2014 1:27 PM
    Moderator
  • Thank you for the reply. 

    The tiles in the gridview contain names of users in the system. They are categorized either according to the first letter of the users name (A,B,C.. etc) or by the category of which the user accounts fall under(Locked, Open... etc)

    What I'm trying to do is to implement a way to switch between the two modes. 

    Right now the gridviews' source is a collection of objects with a key(Which could be first letter or the category name) and a list of user objects under it. 

    I was rebinding the entire OC since I have to change the key in order to change the groups and the keys. Is there a better way to change the groups without having to reload the entire OC? 

    Wednesday, February 12, 2014 2:55 PM
  • I think you might be able to do this easily using a SemanticZoom. 
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.semanticzoom.aspx

    It's not the classic idea of SemanticZoom, but it does present two separate views which you can control.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, February 12, 2014 6:44 PM
    Moderator
  • Unfortunately, I'm already using semantic zoom in both of the views. Therefor I won't be able to use it to mimic two different views. 

    But, like you said completely clearing and reloading the collection seems to be a bad idea. Would it be better if I had two differant gridviews of which one is always hidden? 

    This would mean duplication of the data, but it might help when switching between the views. I'll give it a shot tomorrow. Are there any other way I should try out?

    Thank you again for the responses. 

    Wednesday, February 12, 2014 8:20 PM
  • I think this approach is likely the best way to go about this - there's usually a tradeoff between resources and performance. Unless this causes specific issues, this seems like a good way to get this done.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 13, 2014 1:15 PM
    Moderator