using System.Windows; namespace Artemis.UI.Shared { /// /// Provides a dependency property that can observe the size of an element and apply it to bindings /// public static class SizeObserver { /// /// Gets or sets whether the element should be observed /// public static readonly DependencyProperty ObserveProperty = DependencyProperty.RegisterAttached( "Observe", typeof(bool), typeof(SizeObserver), new FrameworkPropertyMetadata(OnObserveChanged)); /// /// Gets or sets the observed width of the element /// public static readonly DependencyProperty ObservedWidthProperty = DependencyProperty.RegisterAttached( "ObservedWidth", typeof(double), typeof(SizeObserver)); /// /// Gets or sets the observed height of the element /// public static readonly DependencyProperty ObservedHeightProperty = DependencyProperty.RegisterAttached( "ObservedHeight", typeof(double), typeof(SizeObserver)); /// /// Gets whether the provided is being observed /// public static bool GetObserve(FrameworkElement frameworkElement) { return (bool) frameworkElement.GetValue(ObserveProperty); } /// /// Sets whether the provided is being observed /// public static void SetObserve(FrameworkElement frameworkElement, bool observe) { frameworkElement.SetValue(ObserveProperty, observe); } /// /// Gets the observed width of the the provided /// public static double GetObservedWidth(FrameworkElement frameworkElement) { return (double) frameworkElement.GetValue(ObservedWidthProperty); } /// /// Sets the observed width of the the provided /// public static void SetObservedWidth(FrameworkElement frameworkElement, double observedWidth) { frameworkElement.SetValue(ObservedWidthProperty, observedWidth); } /// /// Gets the observed height of the the provided /// public static double GetObservedHeight(FrameworkElement frameworkElement) { return (double) frameworkElement.GetValue(ObservedHeightProperty); } /// /// Sets the observed height of the the provided /// public static void SetObservedHeight(FrameworkElement frameworkElement, double observedHeight) { frameworkElement.SetValue(ObservedHeightProperty, observedHeight); } private static void OnObserveChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { FrameworkElement frameworkElement = (FrameworkElement) dependencyObject; if ((bool) e.NewValue) { frameworkElement.SizeChanged += OnFrameworkElementSizeChanged; UpdateObservedSizesForFrameworkElement(frameworkElement); } else { frameworkElement.SizeChanged -= OnFrameworkElementSizeChanged; } } private static void OnFrameworkElementSizeChanged(object sender, SizeChangedEventArgs e) { UpdateObservedSizesForFrameworkElement((FrameworkElement) sender); } private static void UpdateObservedSizesForFrameworkElement(FrameworkElement frameworkElement) { frameworkElement.SetCurrentValue(ObservedWidthProperty, frameworkElement.ActualWidth); frameworkElement.SetCurrentValue(ObservedHeightProperty, frameworkElement.ActualHeight); } } }