diff --git a/MapControl/WPF/DependencyPropertyHelper.WPF.cs b/MapControl/WPF/DependencyPropertyHelper.WPF.cs index 00a7a444..16007b0d 100644 --- a/MapControl/WPF/DependencyPropertyHelper.WPF.cs +++ b/MapControl/WPF/DependencyPropertyHelper.WPF.cs @@ -71,7 +71,13 @@ namespace MapControl if (changed != null) { - metadata.PropertyChangedCallback = (o, e) => changed((FrameworkElement)o, (TValue)e.OldValue, (TValue)e.NewValue); + metadata.PropertyChangedCallback = (o, e) => + { + if (o is FrameworkElement element) + { + changed(element, (TValue)e.OldValue, (TValue)e.NewValue); + } + }; } return DependencyProperty.RegisterAttached(name, typeof(TValue), typeof(TOwner), metadata); diff --git a/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs b/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs index 469b2056..486de731 100644 --- a/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs +++ b/MapControl/WinUI/DependencyPropertyHelper.WinUI.cs @@ -34,9 +34,15 @@ namespace MapControl Action changed = null, bool inherits = false) // unused in WinUI/UWP { - var metadata = changed != null - ? new PropertyMetadata(defaultValue, (o, e) => changed((FrameworkElement)o, (TValue)e.OldValue, (TValue)e.NewValue)) - : new PropertyMetadata(defaultValue); + var metadata = changed == null + ? new PropertyMetadata(defaultValue) + : new PropertyMetadata(defaultValue, (o, e) => + { + if (o is FrameworkElement element) + { + changed(element, (TValue)e.OldValue, (TValue)e.NewValue); + } + }); return DependencyProperty.RegisterAttached(name, typeof(TValue), typeof(TOwner), metadata); }