// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control // Copyright © 2024 Clemens Fischer // Licensed under the Microsoft Public License (Ms-PL) using System; #pragma warning disable AVP1001 // The same AvaloniaProperty should not be registered twice namespace MapControl { public static class DependencyPropertyHelper { public static StyledProperty Register( string name, TValue defaultValue = default, Action changed = null, Func coerce = null, bool bindTwoWayByDefault = false) where TOwner : AvaloniaObject { Func coerceFunc = null; if (coerce != null) { // do not coerce default value coerceFunc = (obj, value) => Equals(value, defaultValue) ? value : coerce((TOwner)obj, value); } var bindingMode = bindTwoWayByDefault ? Avalonia.Data.BindingMode.TwoWay : Avalonia.Data.BindingMode.OneWay; var property = AvaloniaProperty.Register(name, defaultValue, false, bindingMode, null, coerceFunc); if (changed != null) { property.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); } return property; } public static AttachedProperty RegisterAttached( string name, TValue defaultValue = default, Action changed = null, bool inherits = false) { var property = AvaloniaProperty.RegisterAttached(name, defaultValue, inherits); if (changed != null) { property.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); } return property; } public static StyledProperty AddOwner( StyledProperty property, TValue defaultValue = default, Action changed = null) where TOwner : AvaloniaObject { var newProperty = property.AddOwner(); if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue)) { newProperty.OverrideMetadata(new StyledPropertyMetadata(defaultValue)); } if (changed != null) { newProperty.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); } return newProperty; } public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding) { target.Bind(property, binding); } } }