AvaloniaProperty default value factory
See original GitHub issueIs your feature request related to a problem? Please describe. It should be possible to set default value factory.
Describe the solution you’d like
public static StyledProperty<TValue> Register<TOwner, TValue>(string name, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null, Action<IAvaloniaObject, bool> notifying = null) where TOwner : IAvaloniaObject;
public static AttachedProperty<TValue> RegisterAttached<TOwner, THost, TValue>(string name, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null) where THost : IAvaloniaObject;
public static AttachedProperty<TValue> RegisterAttached<THost, TValue>(string name, Type ownerType, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null) where THost : IAvaloniaObject;
Additional context WPF doesn’t have this feature. But UWP does: CreateDefaultValueCallback Delegate
Property = DependencyProperty.Register("MyProperty",
propertyType, ownerType,
PropertyMetadata.Create(createDefaultValue));
Possible use cases
- Storing mutable objects inside of property, and default value should be new for each avalonia object having it - https://devblogs.microsoft.com/oldnewthing/20191002-00/?p=102950 (actually it’s a pretty BAD idea, and this topic describes why).
- When default value is read from global settings class or any other static source that can be mutated, and default value should be recalculated.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Defining Properties
defaultValue : This gives the property a default value. Be sure to only pass value types and immutable types here as passing a...
Read more >The default value for a XAML dependency property should ...
The default value of a dependency property is the value used if no value has been set: Nobody has explicitly assigned a value,...
Read more >How can a property take the default values defined in ...
I have a class with non-nullable values whose default values I have defined in the constructor. class AirConPreferences { bool allowOn; int ...
Read more >ReactiveProperty documentation
The value property raise the PropertyChanged event. Implements the IObservable<T> interface. Yes, The value property can bind to XAML control's property. And ...
Read more >equadrat.Framework.Core.UI.Maui 5.8.5
- Added extension method GetDefaultValue() for System.Type to get the default value for that type. - Added eCoreDispatcherPriority.Layout as an ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
The UWP implementation has a serious problem: https://devblogs.microsoft.com/oldnewthing/20191002-00/?p=102950
It may be possible for Avalonia to avoid that problem, but it’s a bit tricky.
EDIT: Oh I just noticed you already linked to that, sorry, Anyway I think that unless we can avoid the problem described there I’d be 👎 on adding default value factories.
I would be willing to bet Microsoft has changed the intended use/functionality of this. Originally, it might have been a full factory created to facilitate instantiating objects used on the UI thread from another thread. Then they did some backtracking and clarified in the documents it should only be used for the very narrow case. That could explain why everyone has different impressions and tests dont always match the docs. It could also be that it never worked correctly so they narrowed scope to the only tested and validated use case.
Reguardless, treating this as a factory in Avalonia would be great. It could be used to instantiate collections for each dependency object (equivalent in Avalonia) without doing so in the constructor which is cleaner IMO.