Repository navigation
Replies: 1 comment
|
Not from the arguments. Per property (no reflection): the generator emits partial methods that take both values. Implement the ones you need: [ObservableProperty]
public partial string? Name { get; set; } // or: private string? name;
partial void OnNameChanging(string? oldValue, string? newValue) { }
partial void OnNameChanged(string? oldValue, string? newValue) { }For any property in one place: the generated setter calls public partial class Person : ObservableObject
{
private object? _oldValue;
protected override void OnPropertyChanging(PropertyChangingEventArgs e)
{
_oldValue = GetValue(e.PropertyName); // still the old value here
base.OnPropertyChanging(e);
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
object? newValue = GetValue(e.PropertyName); // already the new value
Console.WriteLine($"{e.PropertyName}: {_oldValue} -> {newValue}");
base.OnPropertyChanged(e);
}
private object? GetValue(string? name) =>
name is null ? null : GetType().GetProperty(name)?.GetValue(this);
}I ran this with CommunityToolkit.Mvvm 8.4.2 and it prints
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Can I get not only the name of any changed property in OnPropertyChanged/OnPropertyChanging, but also its old/new values?
All reactions