DependencyPropertyHelper

This commit is contained in:
ClemensFischer 2024-05-23 18:08:14 +02:00
parent c74c2b1fed
commit 8e82e0bcbd
25 changed files with 224 additions and 176 deletions

View file

@ -9,6 +9,15 @@ namespace MapControl
{
public static class DependencyPropertyHelper
{
public static DependencyProperty Register<TOwner, TValue>(
string name,
TValue defaultValue,
FrameworkPropertyMetadataOptions options)
where TOwner : DependencyObject
{
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), new FrameworkPropertyMetadata(defaultValue, options));
}
public static DependencyProperty Register<TOwner, TValue>(
string name,
TValue defaultValue = default,
@ -64,5 +73,35 @@ namespace MapControl
{
return DependencyProperty.RegisterReadOnly(name, typeof(TValue), typeof(TOwner), new PropertyMetadata(defaultValue));
}
public static DependencyProperty AddOwner<TOwner>(
DependencyProperty property,
FrameworkPropertyMetadataOptions options = FrameworkPropertyMetadataOptions.None)
where TOwner : DependencyObject
{
FrameworkPropertyMetadata metadata = null;
if (options != FrameworkPropertyMetadataOptions.None)
{
metadata = new FrameworkPropertyMetadata(property.DefaultMetadata.DefaultValue, options);
}
return property.AddOwner(typeof(TOwner), metadata);
}
public static DependencyProperty AddOwner<TOwner, TValue>(
DependencyProperty property,
Action<TOwner, TValue, TValue> changed)
where TOwner : DependencyObject
{
FrameworkPropertyMetadata metadata = null;
if (changed != null)
{
metadata = new FrameworkPropertyMetadata((o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue));
}
return property.AddOwner(typeof(TOwner), metadata);
}
}
}