-1

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 :)

Joren Vandamme
  • 381
  • 2
  • 5
  • 17
  • 1
    LocatieModel should implement INotifyPropertyChanged if you want to see updates – ASh Jul 23 '21 at 21:32
  • Also note that `OnPropertyChange("Locaties");` is a typo. Consider using nameof: `OnPropertyChange(nameof(Locations));` – Clemens Jul 24 '21 at 07:00
  • @Clemens Thanks for your message. Does the property name you give with OnPropertyChange, be the same name as the property where you are using this OnPropertyChange? – Joren Vandamme Jul 24 '21 at 08:48

1 Answers1

1

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));
   }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17