Repository navigation
Replies: 2 comments
|
If you need custom getter logic, one option is to keep the observable property as the backing value and expose a separate computed property: [ObservableProperty]
private string? name;
public string DisplayName =>
string.IsNullOrEmpty(Name) ? "Default" : Name;
|
|
Not with For the few properties with custom getter logic, drop the attribute and write the property by hand. With C# 14 (the default on .NET 10, or public string Name
{
get => string.IsNullOrEmpty(field) ? "Fallback" : field;
set => SetProperty(ref field, value);
} = "";
The other option (as suggested above) is to keep the generated property for the raw value and add a separate computed property for the fallback. That is better if bindings should still be able to see the raw value. |
Uh oh!
There was an error while loading. Please reload this page.
I'm converting a legacy codebase to communitytoolkit and am currently facing a few instances where the getter of a proeprty I seek to replace has custom logic (e.g. a fallback value in case the backing field is an empty string). The generated code fia a partial property is
get => field;would it be possible to define the getter in the user code instead?All reactions