Search code examples
wpfmvvmdata-binding

C#WPF - Binding updates on add/remove but not on update


In my WPF application I have this ListView with an edit, add and remove button. Pressing the remove or add button, immediately shows the change in the GUI.

Pressing the Update button, doesn't change the GUI, only when I change to another page (using IPage) and back, the page is refreshed/the change is shown.

I want the view to refresh when I update the property Naam of a location too.

Xaml:

<ListView ItemsSource="{Binding Locations, Mode=TwoWay}" SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Naam}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
<Button Content="Update" Command="{Binding UpdateLocationCommand}" />
<Button Content="Remove" Command="{Binding RemoveLocationCommand}" />
<Button Content="Add" Command="{Binding AddLocationCommand}" />

VM:

class ClassName : ObservableObject, IPage, INotifyPropertyChanged
{
    private ObservableCollection<LocatieModel> _locations = new ObservableCollection<LocatieModel>();
    public ObservableCollection<LocatieModel> Locations
    {
       get { return _locations; }
       set
       {
           _locations = value;
           OnPropertyChange("Locaties");
       }
    }
    private LocatieModel selectedLocation;
    public LocatieModel SelectedLocation
    {
        get { return selectedLocation; }
        set
        {
            selectedLocation= value;
            OnPropertyChange("SelectedLocation");
        }
    }


    public VM()
    {
        addLocationCommand = new Command(AddLocation, true);
        updateLocationCommand = new Command(UpdateLocation, true);
        removeLocationCommand = new Command(RemoveLocation, true);
    }

    private void AddLocation(object obj)
    {
        Locations.Add(new LocatieModel() { Naam = "banana1"});
    }

    private void RemoveLocation(object obj)
    {
        Locations.Remove(SelectedLocation);
    }

    private void UpdateLocation(object obj)
    {
        Locations.Single(l => l == SelectedLocation).Naam = "apple42";
    }


    // Part for InotificationChanged
   public event PropertyChangedEventHandler PropertyChanged;
   private void OnPropertyChange(string propertyname)
   {
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
   }
}

And the LocatieModel, is just a model:

public class LocatieModel 
{
   public int Id { get; set; }
   public string Naam { get; set; }
}

How to make the gui update the list when it's an update? Can you trigger it automaticly? Out of ideas. Thanks for your time.

PS: I looked at possible answer but that makes it a list of invisible(not existing) elements.

EDIT: I tried implementing INotifyPropertyChanged in the LocatieModel class yesterday and that didn't work. Probably an oversight somewhere because now it works. Thanks :)


Solution

  • To update the Naam, you must implement INotifyPropertyChanged in the LocatieModel class.

    LocatieModel to

    public class LocatieModel : INotifyPropertyChanged
    {
       public int Id { get; set; }
    
       private string naam;
    
       public string Naam 
       {
          get { return naam; }
          set
          {
              naam = value;
              OnPropertyChange(nameof(Naam));
          }
       }
    
       public event PropertyChangedEventHandler PropertyChanged;
    
       protected void OnPropertyChange(string propertyName)
       {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
       }
    }