4 Replies - 4307 Views - Last Post: 17 April 2012 - 04:29 PM Rate Topic: -----

#1 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Listbox binding not updating with new data

Posted 15 April 2012 - 09:47 AM

I have all of my data stored in a repository class that contains a list. I have some hard coded data in this list at startup, and my bound list box is populated correctly when I launch the app. But when I try to add a new item via the GUI, the list box is not updating with the new item. I have verified that the new item is added to the repository, and it is brought over to the viewmodel.

The Load Call:
private void LoadRecipes()
        {
            try
            {
                
                this.Recipes = new ObservableCollection<Recipe>(this._recipeRepository.GetRecipes());
                //debug
                DumpRecipes();            
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in LoadRecipes: " + ex.Message);                
            }
        }



The GetRecipes call:
public IEnumerable<Recipe> GetRecipes()
        {
            var query = from e in _recipes orderby e.Id select e;
            return query.ToList();
        }



The XAML:
<ListBox 
                Name="RecipeNames" 
                ItemsSource="{Binding Recipes}"
                Margin="6"     
                SelectedItem="{Binding Path=SelectedRecipe, Mode=TwoWay}"
                Grid.Row="1"
                >



If I add the item right into the ObservableCollection in the viewmodel, the list updates correctly, I just cannot figure out why it is not updating with the above calls.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Listbox binding not updating with new data

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Listbox binding not updating with new data

Posted 16 April 2012 - 06:02 AM

Can you show the entire ViewModel?
Was This Post Helpful? 0
  • +
  • -

#3 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Listbox binding not updating with new data

Posted 16 April 2012 - 12:25 PM


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace GUI
{
    public class MainViewModel : INotifyPropertyChanged
    {
        ObservableCollection<Recipe> _recipesResults;
        Recipe _activeRecipe;
        Recipe _selectedRecipe;
        readonly RecipeRepository _recipeRepository;
        RelayCommand _clearCommand;
        RelayCommand _saveCommand;

        public MainViewModel() : this(new RecipeRepository())
        {

        }

        public MainViewModel(RecipeRepository recipeRepository)
        {
            this._recipeRepository = recipeRepository;
            this.Recipes = new ObservableCollection<Recipe>();
            LoadRecipes();
            this.ActiveRecipe = new Recipe();
        }

        public Recipe ActiveRecipe 
        { 
            get 
            {
                return _activeRecipe; 
            }
            set 
            {
                _activeRecipe = value;
                RaisePropertyChanged("ActiveRecipe");
            } 
        }

        public ObservableCollection<Recipe> Recipes
        {
            get { return _recipesResults; }
            set
            {
                _recipesResults = value;
            }
        }

        public Recipe SelectedRecipe
        {
            get
            {
                return _selectedRecipe;
            }
            set
            {
                _selectedRecipe = value;
                RaisePropertyChanged("SelectedRecipe");
                RecordSelector(value);
            }
        }

        public String RecipeCount 
        { 
            get
            {
                return this.Recipes.Count.ToString();
            }
            set
            {
                RecipeCount = value;
            }
        }

        private void RecordSelector(Recipe value)
        {
            if (value == null) return;
            try
            {
                this.ActiveRecipe = _recipeRepository.GetById(value.Id);
            }
            catch (Exception)
            {
                
            }
        }

        private void LoadRecipes()
        {
            try
            {
                
                this.Recipes = new ObservableCollection<Recipe>(this._recipeRepository.GetRecipes());
                //debug
                DumpRecipes();            
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in LoadRecipes: " + ex.Message);                
            }
        }

        private void DumpRecipes()
        {
            Console.WriteLine("Dumping Recipes: ");
            foreach (Recipe item in this.Recipes)
            {
                Console.WriteLine(item.Id);
                Console.WriteLine(item.Name);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        #region Commands
        public ICommand ClearCommand
        {
            get
            {
                if (_clearCommand == null)
                {
                    _clearCommand = new RelayCommand(param => this.ClearRecipe(), param => this.CanClear);
                }
                return _clearCommand;
            }
        }

        private void ClearRecipe()
        {
            //this.ActiveRecipe = null;
            this.ActiveRecipe = new Recipe();
        }

        public bool CanClear
        {
            get
            {
                if (this.ActiveRecipe.Name != "")
                    return true;
                else
                    return false;
            }
            set
            {
                CanClear = value;
            }
        } 

        public ICommand SaveRecipeCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(param => this.SaveCurrentRecipe(), param => this.CanSaveCurrentRecipe);
                }
                return _saveCommand;
            }
        }

        private void SaveCurrentRecipe()
        {
            _recipeRepository.Save(this.ActiveRecipe);
            //this.Recipes.Add(this.ActiveRecipe);
            this.ActiveRecipe = new Recipe();
            LoadRecipes();
        }
        
        public bool CanSaveCurrentRecipe 
        { 
            get
            {
                if (!this.ActiveRecipe.Name.Equals(""))
                    return true;
                else
                    return false;
            }
        }
        #endregion
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Listbox binding not updating with new data

Posted 17 April 2012 - 05:06 AM

Try this change...

public ObservableCollection<Recipe> Recipes
{
	get { return _recipesResults; }
	set
	{
		_recipesResults = value;
		RaisePropertyChanged("Recipes");
	}
}



From what it seems, when you save a Recipe, you reload the Recipes property. However, you aren't implementing the NotifyPropertyChanged method for this property. Since you aren't doing that, the UI isn't being notified that the property changed.

Any property in the ViewModel that will change from code, needs to call the RaisePropertyChanged method.
Was This Post Helpful? 1
  • +
  • -

#5 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Listbox binding not updating with new data

Posted 17 April 2012 - 04:29 PM

Thank you once again.
Guess I have some more reading to do :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1