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)
Diagram:
diagram.bmp - Google Drive[
^]
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:
What I have tried:
In my views .xaml:
<Button
Content="{Binding content, Mode=OneWay, NotifyOnTargetUpdated=True}"
Command="{Binding action}" />
In my views .xaml.cs (code behind):
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
{
public E MyObject = new E();
...
}
public class D
{
...
}
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 void OnPressed(object commandParameter, string value)
{
Text = "newtext";
_pressed.InvokeCanExecuteChanged();
}
private bool CanExecute(object commandParameter)
{
...
}
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!