locked
How to update the UI gridview when the content of the grid changes dynamically?

    Question

  • Hi,

    I have a gridview whose itemsource is bound to an Observable Dictionary.

         this.objectViewModel["Folder"] =list;

    On a particular event a new object needs to be added to this list and UI should get updated.

    When I do the following,    

    McmObjectList list = (McmObjectList)ObjectViewModel["Folder"];
     list.Add(e.Folder);     

    the UI gridview doesnt get updated.

    But the following works

                     list.Add(obj1);
                    ObjectViewModel["Folder"] = null;
                    ObjectViewModel["Folder"] = list;

    But I do not want to invalidate the list everytime a new item is added to the list.

    How can I update the UI dynamically without setting it to null?

    Wednesday, February 11, 2015 5:36 AM

Answers

  • Yes. If you don't notify the binding that the collection changed it doesn't know to update the view.

    I'm not aware of any sample apps that implement it (most people just use ObservableCollection<T>), but it's pretty trivial. Intellisense will generate the CollectionChanged event for you. You just need to call it when the collection changes and let it know the change (which items were added, removed, etc.).

    Thursday, February 12, 2015 1:10 AM
    Owner
  • If you're firing change notifications and they're not going through then there's probably something wrong wiht the bindings. You'll need to provide more info about how your overall binding structure works. If you can reproduce it in a minimal sample that you can share on your OneDrive that would be very helpful.
    Friday, February 20, 2015 12:47 AM
    Owner

All replies

  • What is McmObjectList? ObservableDictionary is not a standard type for Windows Runtime apps. If you're just adding items and not adding key/value pairs can you make it an ObservableCollection<T>?

    It sounds like McmObjectList does not implement INotifyCollectionChanged and so does not report to the grid that it's updated. If it does implement INotifyCollectionChanged (as the ObservableCollection does) then there's something else hinky in the wiring between that and the GridView binding. In that case you'll need to provide more info about how the overall binding structure works. If so, then if you can reproduce it in a minimal sample that you can share on your OneDrive that would be very helpful.

    --Rob

    Wednesday, February 11, 2015 5:47 AM
    Owner
  • Thanks a lot for your reply.

    McmObjectList is just custom List that Iv created and it doesnt implement INotifyCollectionChanged. So is this the reason why the UI is not getting updated?

    Do you have any sample apps where INotifyCollectionChanged is used?

    Wednesday, February 11, 2015 6:24 AM
  • Yes. If you don't notify the binding that the collection changed it doesn't know to update the view.

    I'm not aware of any sample apps that implement it (most people just use ObservableCollection<T>), but it's pretty trivial. Intellisense will generate the CollectionChanged event for you. You just need to call it when the collection changes and let it know the change (which items were added, removed, etc.).

    Thursday, February 12, 2015 1:10 AM
    Owner
  • Thanks a lot.

    Ive implemented the INotifyCollectionChanged interface. In the add method, Im calling

     this.OnNotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, obj));

       #region INotifyCollectionChanged
            private void OnNotifyCollectionChanged(NotifyCollectionChangedEventArgs args)
            {
                if (this.CollectionChanged != null)
                {
                    this.CollectionChanged(this, args);
                }
            }
            public event NotifyCollectionChangedEventHandler CollectionChanged;
            #endregion INotifyCollectionChanged

    In my xaml.cs file, on a particular event , I have done the following,

               

     McmObjectList list = (McmObjectList)grid.ItemSource;
     list.Add(obj1);          
      But still no luck. :( The gridview is not getting updated with the new object added.

    this.CollectionChanged  is always null, and Im not sure why this is happening.

    Thanks

    Thursday, February 12, 2015 4:37 AM
  • If you're firing change notifications and they're not going through then there's probably something wrong wiht the bindings. You'll need to provide more info about how your overall binding structure works. If you can reproduce it in a minimal sample that you can share on your OneDrive that would be very helpful.
    Friday, February 20, 2015 12:47 AM
    Owner
  • Hi Rob, 

    As suggested by you, I have created a small sample application to reproduce this issue.

    Could you please have a look and let me know what mistake I am doing while binding.

    To access the project : https://onedrive.live.com/redir?resid=C4B31B12F545DC6C!450&authkey=!AFYrAww-OdxSkok&ithint=file%2czip

    Thanks.

    Monday, March 30, 2015 4:30 AM