0

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.

Sagotharan
  • 2,586
  • 16
  • 73
  • 117

2 Answers2

2

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

Community
  • 1
  • 1
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • I have already saw that answer, but they apply Text="This prompt dissappears as you type..." in usercontrol itself. But I want this watermark as property field like with/Height named WaterMark. Then user can Type what they like.. – Sagotharan Nov 11 '13 at 07:03
  • You Can download my Usercontrol http://www.codeproject.com/Tips/678895/Button-Inside-passwordbox-like-Windows-8-in-WPF-Us – Sagotharan Nov 11 '13 at 07:04
  • Darn David, I was searching for that same answer ... I'm quite sure I've seen another very similar solution as well ... :) – Noctis Nov 11 '13 at 07:05
  • Can You Explain this with an example Pls. – Sagotharan Nov 11 '13 at 07:30
0

Try add style for your control:

Resourse:

<SolidColorBrush x:Key="watermarkBackground" Color="White" />
<SolidColorBrush x:Key="watermarkForeground" Color="LightSteelBlue" />
<SolidColorBrush x:Key="watermarkBorder" Color="Indigo" />
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

<Style x:Key="MyStyle" TargetType="Grid" >
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Margin" Value="20,0" />
</Style>

And, how this style use in MainWindow.xaml

<Grid Background="{StaticResource watermarkBackground}" Style="{StaticResource MyStyle}" >
    <TextBlock Text="Your water mark" 
               Foreground="{StaticResource watermarkForeground}"
               Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
    <TextBox  Background="Transparent" BorderBrush="{StaticResource watermarkBorder}" />
</Grid>
Aleksey
  • 1,299
  • 9
  • 13