Search code examples
c#wpfuser-controlswatermark

Want to Add watermark text as a property in WPF User control?


I have created a WPF usercontrol. That control contain one textbox and button. I want a property for water mark in that control.

This will help the user what they like to add the text as watermark. like WaterMark="Enter the Password...".

<wpfCtrl:PasswordBoxWin8 Background="CadetBlue" Margin="24,12,257,258" FontSize="26" />

How Can I add watermark as propery in my user control?

Passwordbox user control download.


Solution

  • Have a look at this for a watermark

    Watermark / hint text TextBox in WPF

    Basically add a text block that sits over you textbox and then hide it when you dont want the watermark shown anymore.

    If you want the custom text, create a dependency property and bind that to the Text property of the textblock. This way, the user can specify whatever text they want.

    public string WaterMark
    {
      get { return (string )this.GetValue(WaterMarkProperty); }
      set { this.SetValue(WaterMarkProperty, value); } 
    }
    public static readonly DependencyProperty WaterMarkProperty = DependencyProperty.Register(
     "WaterMark", typeof(string ), typeof(PasswordBoxWin8));
    

    Then you bind to it in the XAML

    <TextBlock Text="{Binding WaterMark, ElementName=YourUserControlName}" />
    

    THis way, your user control has a property called WaterMark that you can set