Im working with MVVM, i have a ClientClass object with many properties (~50). So, in my view i have a Picker
that is binded to the Client.ClientType
property, and the same property binded to an Image
icon like, that change depending on the Client.ClientType
value. The problem is if i change that value, the OnPropertyChanged
is not triggering, it only does if Client
is assigned a whole new value like Client = new ClientClass()
.
I know i can wrap the property and force it like this:
public int ClientType {
get => client.ClientType;
set {
if (client.ClientType == value) return;
client.ClientType = value;
OnPropertyChanged(nameof(Client));
}
}
but i wont do it for all properties.
So, there is a “simple” way to make all properties be like this without declare each one separately?
Simplified code example:
public class BaseVM : INotifyPropertyChanged {
bool isBusy;
string? title;
public bool IsBusy {
get => isBusy;
set {
if (isBusy == value) return;
isBusy = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler? PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string? name = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public class ClientEditorVM : BaseVM {
private ClientClass client;
public ClientClass Client{
get => client;
set {
if (client.Equals(value)) return;
client = value;
OnPropertyChanged();
}
}
// this works, but repeat 50 times??
public int ClientType {
get => client.ClientType;
set {
if (client.ClientType == value) return;
client.ClientType = value;
OnPropertyChanged(nameof(Client));
}
}
public ClientEditorVM(ClientClass c){
Client = c;
}
}
public class ClientClass {
public int ID { get; }
public int ClientType { get; set; }
public string Name { get; set; }
public string Address { get; set; }
...
// like 50 properties with no OnPropertyChanged implementation. Cant modify this.
}