Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DirectProperty does not use its setter when new value is the same as old value #17963

Open
ent3m opened this issue Jan 13, 2025 · 0 comments
Open
Labels

Comments

@ent3m
Copy link

ent3m commented Jan 13, 2025

Describe the bug

I have a DirectProperty that is bound to an ObservableObject's property in my project. Here is an example of a DirectProperty implementing some custom logic in its setter. I expect the setter code to run whenever the ObservableObject raises a PropertyChanged event, but that only happens when newValue does not equal oldValue according to the default IEqualityComparer. I know that because my custom logic does not run. In fact, I suspect that the value held by the backing field does not even get used.

public static readonly DirectProperty<ControlWithDirectProperty, string?> DirectValueProperty =
AvaloniaProperty.RegisterDirect<ControlWithDirectProperty, string?>(nameof(DirectValue), o => o.DirectValue, (o, e) => o.DirectValue = e);
public string? DirectValue
{
get => _directValue;
set
{
/* some custom logic */
SetAndRaise(DirectValueProperty, ref _directValue, value);
}
}
private string? _directValue;

To Reproduce

Create a control with a DirectProperty that has a binding and some custom logic in the setter. Raise the PropertyChanged event in the binding source while not giving a different new value. See that the custom setter logic does not execute. You can download my sample project to test it out.
AvaloniaDirectPropertyTest.zip

Expected behavior

The DirectProperty's setter should be executed regardless of what the new value is. Equality check should be done by comparing the new value against the backing field, and the decision to update the value and raise PropertyChanged event should be done within the SetAndRaise method.

Avalonia version

11.2.3

OS

Windows

Additional context

The documentation states that Avalonia DirectProperty "expose a standard C# property as an Avalonia property", and that the "property setter is a standard C# property setter that raises an event". Equality check should be done by comparing against the backing field within DirectProperty's setter SetAndRaise method, as written in the source code, to ensure that the user has full control over how the property is set and get, and that any custom logic is respected. How can I ensure that the setter code for my property is always executed?

@ent3m ent3m added the bug label Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant