global using DependencyProperty = Avalonia.AvaloniaProperty; using Avalonia; using Avalonia.Controls; using Avalonia.Data; using System; #pragma warning disable AVP1001 namespace MapControl { public static class DependencyPropertyHelper { public static AttachedProperty RegisterAttached( string name, Type ownerType, TValue defaultValue = default, Action changed = null, bool inherits = false) { var property = AvaloniaProperty.RegisterAttached(name, ownerType, defaultValue, inherits); if (changed != null) { property.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); } return property; } 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 ? BindingMode.TwoWay : 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 void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding) { target.Bind(property, binding); } } }