locked
Can anyone supply me with a simple code to show how to update UI at runtime using INotifyCollectionChanged ?

    Question

  • i only know how to use INotifyPropertyChanged which i am struggling to update and item with a collection inside ?

    19karabo91

    Tuesday, February 18, 2014 1:42 PM

Answers

  • public Task Add(Board board)
    		{
    			var category = categories.Where(o => o.Title == board.Category).FirstOrDefault();
    			if (category != null)
    			{
    				var existingBoard = category.Items.Where(o => o.Title == board.Title && o.Category == board.Category).FirstOrDefault();
    				if (existingBoard == null)
    					category.Items.Add(board);
    			}
    			else
    			{
    				BoardCategory newCategory = new BoardCategory();
    				newCategory.Title = board.Category;
    				newCategory.Items = new ObservableCollection<Board>();
    				newCategory.Items.Add(board);
    
    				categories.Add(newCategory);
    			}
    			return WriteToFile();
    		}

    solution is pretty easy and mentioned here above. use observablecollection, also in the BoardCategory class so change the BoardCategory class to:

    public class BoardCategory
    	{
    		public string Title { get; set; }
    		public ObservableCollection<Board> Items { get; set; }
    	}

    ofcourse need to change the addboard method in the repository aswell


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by 19karabo91 Wednesday, February 19, 2014 9:40 AM
    Wednesday, February 19, 2014 9:25 AM

All replies

  • Public class BoardCategory
    {
    public string Title {get;set;}
    public List<Board> {get;set;}
    }

    i only know how to use INotifyPropertyChanged which i am struggling to update and item with a collection inside , i am binding my UI with this class 



    19karabo91

    Tuesday, February 18, 2014 1:49 PM
  • Can you show us a sample of what your object looks like and how you are binding to it? What type is your collection? We can't show you how to achieve your goal without better understanding what you are trying to do.

    That said, if you want to notify when items are added and removed from the collection make it an ObservableCollection<T> instead of a List<T>

    --Rob

    Tuesday, February 18, 2014 2:51 PM
    Moderator
  • The collection class binds to a grid view with an observableCollection

    19karabo91

    Tuesday, February 18, 2014 2:58 PM
  • Make sure you are loading the ObservableCollection by clearing and adding new items to it rather than setting the property to a new instance of an ObservableCollection.  If you set the property to a new instance, the data binding in your view will lose the reference to the collection.
    Tuesday, February 18, 2014 4:08 PM
  • Hi Rob sorry for taking so long here's a link to my  sky drive project please check it out , Thanks .

    19karabo91


    • Edited by 19karabo91 Wednesday, February 19, 2014 9:15 AM
    Wednesday, February 19, 2014 9:14 AM
  • public Task Add(Board board)
    		{
    			var category = categories.Where(o => o.Title == board.Category).FirstOrDefault();
    			if (category != null)
    			{
    				var existingBoard = category.Items.Where(o => o.Title == board.Title && o.Category == board.Category).FirstOrDefault();
    				if (existingBoard == null)
    					category.Items.Add(board);
    			}
    			else
    			{
    				BoardCategory newCategory = new BoardCategory();
    				newCategory.Title = board.Category;
    				newCategory.Items = new ObservableCollection<Board>();
    				newCategory.Items.Add(board);
    
    				categories.Add(newCategory);
    			}
    			return WriteToFile();
    		}

    solution is pretty easy and mentioned here above. use observablecollection, also in the BoardCategory class so change the BoardCategory class to:

    public class BoardCategory
    	{
    		public string Title { get; set; }
    		public ObservableCollection<Board> Items { get; set; }
    	}

    ofcourse need to change the addboard method in the repository aswell


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by 19karabo91 Wednesday, February 19, 2014 9:40 AM
    Wednesday, February 19, 2014 9:25 AM
  • Dave thank you so much ,and its so embarrassing  because I've been stuck with this for two days .your the best .

    19karabo91

    Wednesday, February 19, 2014 9:37 AM