0

I'm doing a UWP project, something similar to an order app, and now I'm kinda stuck on my Restock page.

In the app, I can restock my Stock property, but when I change views, or between tabs, it resets. I tried to make everything static but then im diggin my hole even more with more issues, then I got a tip about INotifyPropertyChanged, but with the examples I've seen I cant make it to work. I have my property in a Merchandise class, which I can get to my Restock xaml.cs file. But I guess I should make something in the class where the property is right?

public class Merchandise
{
     public string Name { get; set; }
     public string Supplier { get; set; }
     public int Stock { get; set; }
     public int MerchandiseId { get; set; }
}

Do I make Merchandise implement the interface?

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
CptDubb
  • 31
  • 6
  • 2
    Does this answer your question? [Implementing INotifyPropertyChanged - does a better way exist?](https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) – Klaus Gütter Nov 02 '20 at 09:11

1 Answers1

1

You should do something like this:

public class Merchandise : INotifyPropertyChanged {

      private string _name;

      public string Name { get { return _name; } set { _name = value; OnPropertyChanged(); } }

      protected void OnPropertyChanged([CallerMemberName] string propertyName = null) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }

      public event PropertyChangedEventHandler PropertyChanged;
    }

But, you should consider some more generic approach, as you will have more objects and/or properties to be changed via UI.

Danny Schneider
  • 311
  • 2
  • 9
  • How do you mean a more generic approach? If i may ask – CptDubb Nov 02 '20 at 09:22
  • Using a base class or a separate view model which you can simple made use of. The linked post from @KlausGütter gives a good explanation... – Danny Schneider Nov 02 '20 at 09:28
  • I tried basiclly ur code code example but in the "set{_stock = value; OnPropertyChanged();}" it wants me to have some sort of parameter, but what parameter? – CptDubb Nov 02 '20 at 09:38
  • it does not want a parameter, [CallerMemberName] automatically inserts the property name. unless you are using a very old version of .NET/C#, where you would pass the property name as a string – Markus Dresch Nov 02 '20 at 09:42
  • @MarkusDresch so i've been trying out this now. And i did read some more regarding the bindings. Do i have to do this step with INotifyPropertyChanged in every class that uses the Stock property, or is it enough in my Merchandise class? – CptDubb Nov 02 '20 at 10:21
  • you have to do this everywhere where you want to informt the UI that a change has happened. the UI listens for the PropertyChanged event. and by invoking that event you tell the UI that some value has to be updated. a ViewModel is the representation of the data in a view. to handle updates in the View you invoke PropertyChanged. – Markus Dresch Nov 02 '20 at 10:40
  • @MarkusDresch So, what do i implement in my Restock.xaml.cs file? Since i dont have the properties there, do i have to implement them AGAIN? Since my code i "fetch" my list from a MerchandiseManager class where my list is. I cant really explain it since more or less just followed a YT tutorial and it has worked since – CptDubb Nov 02 '20 at 11:28
  • 1
    For better understanding uwp binding, please refer [uwp binding depth](https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-in-depth#binding-source) document. And if you don't want to implement `OnPropertyChanged` anywhere, please make viewmodelbase class, please make your viewmodel inherit viewmodelbase class. – Nico Zhu Nov 02 '20 at 12:01