using System.Windows.Controls; // ReSharper disable once CheckNamespace namespace WPFluent.Controls; /// /// Represents an image with additional properties for Borders and Rounded corners /// public class Image : Control { /// /// Identifies the dependency property. /// public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register( nameof(CornerRadius), typeof(CornerRadius), typeof(Image), new PropertyMetadata(new CornerRadius(0), new PropertyChangedCallback(OnCornerRadiusChanged))); /// /// Identifies the dependency property. /// public static readonly DependencyPropertyKey InnerCornerRadiusPropertyKey = DependencyProperty.RegisterReadOnly( nameof(InnerCornerRadius), typeof(CornerRadius), typeof(Image), new PropertyMetadata(new CornerRadius(0))); /// /// Identifies the dependency property. /// public static readonly DependencyProperty InnerCornerRadiusProperty = InnerCornerRadiusPropertyKey.DependencyProperty; /// /// Identifies the dependency property. /// public static readonly DependencyProperty SourceProperty = DependencyProperty.Register( nameof(Source), typeof(ImageSource), typeof(Image), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, null, null), null); /// /// Identifies the dependency property. /// public static readonly DependencyProperty StretchDirectionProperty = DependencyProperty.Register( nameof(StretchDirection), typeof(StretchDirection), typeof(Image), new FrameworkPropertyMetadata( StretchDirection.Both, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), null); /// /// Identifies the dependency property. /// /// public static readonly DependencyProperty StretchProperty = DependencyProperty.Register( nameof(Stretch), typeof(Stretch), typeof(Image), new FrameworkPropertyMetadata( Stretch.Uniform, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender), null); private static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var thickness = (Thickness)d.GetValue(BorderThicknessProperty); var outerRarius = (CornerRadius)e.NewValue; // Inner radius = Outer radius - thickenss/2 d.SetValue( InnerCornerRadiusPropertyKey, new CornerRadius( topLeft: Math.Max(0, (int)Math.Round(outerRarius.TopLeft - thickness.Left / 2, 0)), topRight: Math.Max(0, (int)Math.Round(outerRarius.TopRight - thickness.Top / 2, 0)), bottomRight: Math.Max(0, (int)Math.Round(outerRarius.BottomRight - thickness.Right / 2, 0)), bottomLeft: Math.Max(0, (int)Math.Round(outerRarius.BottomLeft - thickness.Bottom / 2, 0)))); } /// /// Gets the CornerRadius for the inner image's Mask. /// internal CornerRadius InnerCornerRadius => (CornerRadius)GetValue(InnerCornerRadiusProperty); /// /// Gets or sets the CornerRadius property allows users to control the roundness of the corners independently by /// setting a radius value for each corner. Radius values that are too large are scaled so that they smoothly blend /// from corner to corner. /// public CornerRadius CornerRadius { get => (CornerRadius)GetValue(CornerRadiusProperty); set => SetValue(CornerRadiusProperty, value); } /// /// Gets or sets the Source on this Image. The Source property is the ImageSource that holds the actual image drawn. /// public ImageSource Source { get => (ImageSource)GetValue(SourceProperty); set => SetValue(SourceProperty, value); } /// /// Gets or sets the Stretch on this Image. The Stretch property determines how large the Image will be drawn. /// public Stretch Stretch { get => (Stretch)GetValue(StretchProperty); set => SetValue(StretchProperty, value); } /// /// Gets or sets the stretch direction of the Viewbox, which determines the restrictions on scaling that are applied /// to the content inside the Viewbox. For instance, this property can be used to prevent the content from being /// smaller than its native size or larger than its native size. /// public StretchDirection StretchDirection { get => (StretchDirection)GetValue(StretchDirectionProperty); set => SetValue(StretchDirectionProperty, value); } }