2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2024-05-20 23:24:34 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class DependencyPropertyHelper
|
|
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
public static DependencyProperty RegisterAttached<TValue>(
|
|
|
|
|
|
string name,
|
|
|
|
|
|
Type ownerType,
|
|
|
|
|
|
TValue defaultValue = default,
|
|
|
|
|
|
Action<FrameworkElement, TValue, TValue> changed = null,
|
|
|
|
|
|
bool inherits = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
var metadata = new FrameworkPropertyMetadata
|
|
|
|
|
|
{
|
|
|
|
|
|
DefaultValue = defaultValue,
|
|
|
|
|
|
Inherits = inherits
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (changed != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata.PropertyChangedCallback = (o, e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (o is FrameworkElement element)
|
|
|
|
|
|
{
|
|
|
|
|
|
changed(element, (TValue)e.OldValue, (TValue)e.NewValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return DependencyProperty.RegisterAttached(name, typeof(TValue), ownerType, metadata);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static DependencyProperty RegisterAttached<TValue>(
|
|
|
|
|
|
string name,
|
|
|
|
|
|
Type ownerType,
|
|
|
|
|
|
TValue defaultValue,
|
|
|
|
|
|
FrameworkPropertyMetadataOptions options)
|
|
|
|
|
|
{
|
|
|
|
|
|
var metadata = new FrameworkPropertyMetadata(defaultValue, options);
|
|
|
|
|
|
|
|
|
|
|
|
return DependencyProperty.RegisterAttached(name, typeof(TValue), ownerType, metadata);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-23 18:08:14 +02:00
|
|
|
|
public static DependencyProperty Register<TOwner, TValue>(
|
|
|
|
|
|
string name,
|
|
|
|
|
|
TValue defaultValue,
|
|
|
|
|
|
FrameworkPropertyMetadataOptions options)
|
|
|
|
|
|
where TOwner : DependencyObject
|
|
|
|
|
|
{
|
2024-05-27 11:05:22 +02:00
|
|
|
|
var metadata = new FrameworkPropertyMetadata(defaultValue, options);
|
|
|
|
|
|
|
|
|
|
|
|
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
|
2024-05-23 18:08:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
public static DependencyProperty Register<TOwner, TValue>(
|
|
|
|
|
|
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 : DependencyObject
|
|
|
|
|
|
{
|
|
|
|
|
|
var metadata = new FrameworkPropertyMetadata
|
|
|
|
|
|
{
|
|
|
|
|
|
DefaultValue = defaultValue,
|
|
|
|
|
|
BindsTwoWayByDefault = bindTwoWayByDefault
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
if (changed != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata.PropertyChangedCallback = (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (coerce != null)
|
2024-05-20 23:24:34 +02:00
|
|
|
|
{
|
2024-05-21 13:51:10 +02:00
|
|
|
|
metadata.CoerceValueCallback = (o, v) => coerce((TOwner)o, (TValue)v);
|
2024-05-20 23:24:34 +02:00
|
|
|
|
}
|
2024-05-21 13:51:10 +02:00
|
|
|
|
|
2024-05-20 23:24:34 +02:00
|
|
|
|
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-21 13:51:10 +02:00
|
|
|
|
public static DependencyPropertyKey RegisterReadOnly<TOwner, TValue>(
|
|
|
|
|
|
string name,
|
|
|
|
|
|
TValue defaultValue = default)
|
|
|
|
|
|
where TOwner : DependencyObject
|
|
|
|
|
|
{
|
|
|
|
|
|
return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue));
|
|
|
|
|
|
}
|
2024-05-23 18:08:14 +02:00
|
|
|
|
|
|
|
|
|
|
public static DependencyProperty AddOwner<TOwner, TValue>(
|
|
|
|
|
|
DependencyProperty property,
|
2024-07-05 09:20:30 +02:00
|
|
|
|
TValue defaultValue = default,
|
2024-05-24 15:14:05 +02:00
|
|
|
|
Action<TOwner, TValue, TValue> changed = null)
|
2024-05-23 18:08:14 +02:00
|
|
|
|
where TOwner : DependencyObject
|
|
|
|
|
|
{
|
2024-07-05 09:20:30 +02:00
|
|
|
|
var metadata = new FrameworkPropertyMetadata();
|
|
|
|
|
|
|
|
|
|
|
|
if (!Equals(defaultValue, property.GetMetadata(typeof(TOwner)).DefaultValue))
|
|
|
|
|
|
{
|
|
|
|
|
|
metadata.DefaultValue = defaultValue;
|
|
|
|
|
|
}
|
2024-05-23 18:08:14 +02:00
|
|
|
|
|
|
|
|
|
|
if (changed != null)
|
|
|
|
|
|
{
|
2024-07-05 09:20:30 +02:00
|
|
|
|
metadata.PropertyChangedCallback = (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue);
|
2024-05-23 18:08:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return property.AddOwner(typeof(TOwner), metadata);
|
|
|
|
|
|
}
|
2024-05-20 23:24:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|