I have a WPF code in which I can not get notified property updates. My code needs to have such structure:
A1… An (view .xaml & .xaml.cs) → B1… Bn (viewmodel .cs) C (model .cs, my business logic) D (utility .cs like constants etc) E (custom object .cs, that will be x-times instantiated in my model C)
What I need is that ONLY my custom object implements the InotifyPropertyChanged, and NOT my viewmodels, but I couldn’t get the Gui notified when settig the properties of my custom objects (E) inside my models (C). Here is an example of what I have so far:
public A1() { InitializeComponent(); DataContext = new B1(); }
In my viewmodels .cs:
public class B1 : C { public string content => MyObject.Text; public ICommand action => MyObject.Pressed; }
In my models .cs:
public class C : D { // here i instantiate my object(s) public E MyObject = new E();
// here comes my stuff ... }
public class D { //my constants and wrappers ... }
And for my custom objects (they will be x-times instantiated in my model):
public class E : INotifyPropertyChanged { private string _text = String.Empty; public string Text { get => _text set => SetProperty(ref _text, value); }
private readonly _DelegateCommand _pressed; public ICommand Pressed => _pressed;
private bool CanExecute(object commandParameter) { ... // some logic here }
public E() { _pressed = new _DelegateCommand(OnPressed, CanExecute); }
public event PropertyChangedEventHandler PropertyChanged;
public bool SetProperty<T>(ref T field, T newValue, [CallerMemberName]string propertyName = null) { if (!EqualityComparer<T>.Default.Equals(field, newValue)) { field = newValue; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); return true; } return false; } }
Can anyone help me please on this? Thanks a lot in advance for any hints! (PS: Ihr könnt mich gerne auch auf deutsch beantworten!)