2025-08-19 19:43:02 +02:00
|
|
|
|
using Avalonia;
|
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
|
using Avalonia.Data;
|
|
|
|
|
|
using System;
|
2024-05-20 23:24:34 +02:00
|
|
|
|
|
2025-06-10 15:25:17 +02:00
|
|
|
|
#pragma warning disable AVP1001
|
2024-05-21 14:39:53 +02:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class DependencyPropertyHelper
|
|
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
public static AttachedProperty<TValue> RegisterAttached<TValue>(
|
|
|
|
|
|
string name,
|
|
|
|
|
|
Type ownerType,
|
|
|
|
|
|
TValue defaultValue = default,
|
|
|
|
|
|
Action<Control, TValue, TValue> changed = null,
|
|
|
|
|
|
bool inherits = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var property = AvaloniaProperty.RegisterAttached<Control, TValue>(name, ownerType, defaultValue, inherits);
|
|
|
|
|
|
|
|
|
|
|
|
if (changed != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
property.Changed.AddClassHandler<Control, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return property;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static StyledProperty<TValue> Register<TOwner, TValue>(
|
2024-05-20 23:24:34 +02:00
|
|
|
|
string name,
|
|
|
|
|
|
TValue defaultValue = default,
|
2024-05-21 13:51:10 +02:00
|
|
|
|
Action<TOwner, TValue, TValue> changed = null,
|
2024-05-23 18:22:52 +02:00
|
|
|
|
Func<TOwner, TValue, TValue> coerce = null,
|
|
|
|
|
|
bool bindTwoWayByDefault = false)
|
2024-05-20 23:24:34 +02:00
|
|
|
|
where TOwner : AvaloniaObject
|
|
|
|
|
|
{
|
2024-05-24 22:00:16 +02:00
|
|
|
|
Func<AvaloniaObject, TValue, TValue> coerceFunc = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (coerce != null)
|
|
|
|
|
|
{
|
2025-01-27 19:30:02 +01:00
|
|
|
|
// Do not coerce default value.
|
|
|
|
|
|
//
|
2024-05-26 20:32:29 +02:00
|
|
|
|
coerceFunc = (obj, value) => Equals(value, defaultValue) ? value : coerce((TOwner)obj, value);
|
2024-05-24 22:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-10 23:44:26 +02:00
|
|
|
|
var bindingMode = bindTwoWayByDefault ? BindingMode.TwoWay : BindingMode.OneWay;
|
2024-05-24 22:00:16 +02:00
|
|
|
|
|
|
|
|
|
|
var property = AvaloniaProperty.Register<TOwner, TValue>(name, defaultValue, false, bindingMode, null, coerceFunc);
|
2024-05-20 23:24:34 +02:00
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (changed != null)
|
2024-05-20 23:24:34 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
property.Changed.AddClassHandler<TOwner, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
|
2024-05-20 23:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return property;
|
|
|
|
|
|
}
|
2024-05-24 15:14:05 +02:00
|
|
|
|
|
2024-05-27 11:05:22 +02:00
|
|
|
|
public static void SetBinding(this AvaloniaObject target, AvaloniaProperty property, Binding binding)
|
|
|
|
|
|
{
|
|
|
|
|
|
target.Bind(property, binding);
|
|
|
|
|
|
}
|
2024-05-20 23:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|