Reputation: 2626
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.
Upvotes: 0
Views: 2844
Reputation: 1347
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>
Upvotes: 0
Reputation: 13630
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
Upvotes: 2