public WatermarkAdorner(UIElement adornedElement) : base(adornedElement) { if (adornedElement is TextBox) { adornedTextBox = adornedElement as TextBox; adornedTextBox.TextChanged += (s1, e1) => { this.SetWatermarkVisible(true); }; adornedTextBox.GotFocus += (s1, e1) => { this.SetWatermarkVisible(true); }; adornedTextBox.LostFocus += (s1, e1) => { this.SetWatermarkVisible(false); }; _visuals = new VisualCollection(this); textBlock = new TextBlock() { HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Center, Text = WatermarkAdorner.GetWatermark(adornedElement), Foreground = new SolidColorBrush(Color.FromRgb(153, 153, 153)), Margin = new Thickness(5, 0, 2, 0), }; _visuals.Add(textBlock); this.showModel = WatermarkAdorner.GetWatermarkShowMode(adornedElement); } this.IsHitTestVisible = false; }
public WatermarkAdorner(UIElement adornedElement) : base(adornedElement) { if (adornedElement is TextBox) { adornedTextBox = adornedElement as TextBox; adornedTextBox.TextChanged += (s1, e1) => { this.SetWatermarkVisible(true); }; adornedTextBox.GotFocus += (s1, e1) => { this.SetWatermarkVisible(true); }; adornedTextBox.LostFocus += (s1, e1) => { this.SetWatermarkVisible(false); }; adornedTextBox.IsVisibleChanged += (o, e) => { if (string.IsNullOrEmpty(this.adornedTextBox.Text)) { this.textBlock.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Collapsed; } else { this.textBlock.Visibility = Visibility.Collapsed; } }; _visuals = new VisualCollection(this); textBlock = new TextBlock() { HorizontalAlignment = adornedTextBox.HorizontalContentAlignment, VerticalAlignment = adornedTextBox.VerticalContentAlignment, Text = WatermarkAdorner.GetWatermark(adornedElement), Foreground = new SolidColorBrush(Color.FromRgb(153, 153, 153)), Margin = new Thickness(5, 0, 2, 0), }; _visuals.Add(textBlock); this.showModel = WatermarkAdorner.GetWatermarkShowMode(adornedElement); } this.IsHitTestVisible = false; }