Ejemplo n.º 1
0
        /// <summary>
        /// Simple method to add a new TextBlock into the visual tree of the PasswordBox which will present the watermark.
        /// </summary>
        /// <param name="pbox">The target PasswordBox.</param>
        /// <param name="text">The watermark text.</param>
        private static void AddWatermarkElement(PasswordBox pbox, string text)
        {
            var watermarkTextBlock = pbox.FindVisualChildByName<TextBlock>(pbox.Name+pbox.Name+WatermarkId);

            if (watermarkTextBlock == null)
            {
                var fe = pbox.FindVisualChildByName<ScrollViewer>("ContentElement");
                if (fe != null)
                {
                    var panelOwner = fe.FindVisualParent<Panel>();
                    if (panelOwner != null)
                    {
                        // Add the TextBlock
                        var textBlock = new TextBlock
                        {
                            Name = pbox.Name + WatermarkId,
                            Text = text,
                            TextAlignment = TextAlignment.Center,
                            HorizontalAlignment = HorizontalAlignment.Center,
                            VerticalAlignment = VerticalAlignment.Center,
                            Margin = new Thickness(3, 0, 0, 0),
                            Foreground = new SolidColorBrush(Colors.Gray)
                        };
                        int index = panelOwner.Children.IndexOf(fe);
                        panelOwner.Children.Insert(index + 1, textBlock);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Simple method to remove the TextBlock from the PasswordBox visual tree.
 /// </summary>
 /// <param name="pbox">The target PasswordBox.</param>
 private static void RemoveWatermarkElement(PasswordBox pbox)
 {
     var watermarkTextBlock = pbox.FindVisualChildByName<TextBlock>(pbox.Name + WatermarkId);
     if (watermarkTextBlock != null)
     {
         var panelOwner = watermarkTextBlock.FindVisualParent<Panel>();
         if (panelOwner != null)
         {
             panelOwner.Children.Remove(watermarkTextBlock);
         }
     }
 }