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 StyledProperty AddOwner( StyledProperty source) where TOwner : AvaloniaObject { return source.AddOwner(); } public static StyledProperty AddOwner( StyledProperty source, TValue defaultValue) where TOwner : AvaloniaObject { return source.AddOwner(new StyledPropertyMetadata(new Optional(defaultValue))); } public static StyledProperty AddOwner( StyledProperty source, Action changed) where TOwner : AvaloniaObject { var property = source.AddOwner(); property.Changed.AddClassHandler((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value)); return property; } public static StyledProperty AddOwner( string _, // for compatibility with WinUI/UWP DependencyPropertyHelper StyledProperty source) where TOwner : AvaloniaObject { return AddOwner(source); } public static StyledProperty AddOwner( string _, // for compatibility with WinUI/UWP DependencyPropertyHelper StyledProperty source, Action changed) where TOwner : AvaloniaObject { return AddOwner(source, changed); } public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding) { target.Bind(property, binding); } } }