using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; namespace Sicentury.Core.AttachedProperties { /// /// Adorner for the watermark /// internal class WatermarkAdorner : Adorner { #region Private Fields /// /// that holds the watermark /// private readonly ContentPresenter _contentPresenter; #endregion #region Constructor /// /// Initializes a new instance of the class /// /// to be adorned /// The watermark public WatermarkAdorner(UIElement adornedElement, object watermark) : base(adornedElement) { IsHitTestVisible = false; _contentPresenter = new ContentPresenter { Content = watermark, Opacity = 0.3, Margin = new Thickness(Control.Margin.Left + Control.Padding.Left, Control.Margin.Top + Control.Padding.Top, 0, 0) }; if (Control is ItemsControl && !(Control is ComboBox)) { _contentPresenter.VerticalAlignment = VerticalAlignment.Center; _contentPresenter.HorizontalAlignment = HorizontalAlignment.Center; } // Hide the control adorner when the adorned element is hidden var binding = new Binding("IsVisible") { Source = adornedElement, Converter = new BooleanToVisibilityConverter() }; SetBinding(VisibilityProperty, binding); } #endregion #region Protected Properties /// /// Gets the number of children for the . /// protected override int VisualChildrenCount => 1; #endregion #region Private Properties /// /// Gets the control that is being adorned /// private Control Control => (Control)AdornedElement; #endregion #region Protected Overrides /// /// Returns a specified child for the parent . /// /// A 32-bit signed integer that represents the index value of the child . The value of index must be between 0 and - 1. /// The child . protected override Visual GetVisualChild(int index) { return _contentPresenter; } /// /// Implements any custom measuring behavior for the adorner. /// /// A size to constrain the adorner to. /// A object representing the amount of layout space needed by the adorner. protected override Size MeasureOverride(Size constraint) { // Here's the secret to getting the adorner to cover the whole control _contentPresenter.Measure(Control.RenderSize); return Control.RenderSize; } /// /// When overridden in a derived class, positions child elements and determines a size for a derived class. /// /// The final area within the parent that this element should use to arrange itself and its children. /// The actual size used. protected override Size ArrangeOverride(Size finalSize) { _contentPresenter.Arrange(new Rect(finalSize)); return finalSize; } #endregion } }