XAML-Map-Control/MapControl/Avalonia/DependencyPropertyHelper.Avalonia.cs

56 lines
2 KiB
C#
Raw Normal View History

2024-05-20 23:24:34 +02:00

using Avalonia.Controls;
using System;
namespace MapControl
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("AvaloniaProperty", "AVP1001")]
public static class DependencyPropertyHelper
{
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,
bool bindTwoWayByDefault = false,
2024-05-21 13:51:10 +02:00
Action<TOwner, TValue, TValue> changed = null,
Func<TOwner, TValue, TValue> coerce = null)
2024-05-20 23:24:34 +02:00
where TOwner : AvaloniaObject
{
2024-05-21 13:51:10 +02:00
var property = AvaloniaProperty.Register<TOwner, TValue>(name, defaultValue, false,
bindTwoWayByDefault ? Avalonia.Data.BindingMode.TwoWay : Avalonia.Data.BindingMode.OneWay, null,
coerce != null ? ((obj, value) => coerce((TOwner)obj, value)) : null);
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-21 13:51:10 +02:00
public static AttachedProperty<TValue> RegisterAttached<TOwner, TValue>(
2024-05-20 23:24:34 +02:00
string name,
TValue defaultValue = default,
bool inherits = false,
2024-05-21 13:51:10 +02:00
Action<Control, TValue, TValue> changed = null)
2024-05-20 23:24:34 +02:00
where TOwner : AvaloniaObject
{
2024-05-21 13:51:10 +02:00
var property = AvaloniaProperty.RegisterAttached<TOwner, Control, TValue>(name, defaultValue, inherits);
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<Control, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
2024-05-20 23:24:34 +02:00
}
return property;
}
2024-05-21 13:51:10 +02:00
public static DirectProperty<TOwner, TValue> RegisterReadOnly<TOwner, TValue>(
string name,
Func<TOwner, TValue> getter)
where TOwner : AvaloniaObject
{
return AvaloniaProperty.RegisterDirect(name, getter);
}
2024-05-20 23:24:34 +02:00
}
}