WaterMark or hint text in WPF by using DependencyPropertyHi, In this article I will try to explain how to add the watermark or hint text in the textbox in WPF application by using DependencyProperty.
First of all add one class in the solution and put the code as below:
using System.Windows;
using System.Windows.Controls;
namespace WM
{
public static class WaterMark
{
public static readonly DependencyProperty IsWMEnabled =
DependencyProperty.RegisterAttached("IsWMEnabled",
typeof(bool), typeof(WaterMark),
new UIPropertyMetadata(false, OnIsWMEnabled));
public static readonly DependencyProperty WMText =
DependencyProperty.RegisterAttached("WMText",
typeof(string), typeof(WaterMark),
new UIPropertyMetadata(string.Empty, OnWMTextChanged));
public static string GetWMText(DependencyObject obj)
{
return (string)obj.GetValue(WMText);
}
public static void SetWMText(DependencyObject obj, string value)
{
obj.SetValue(WMText, value);
}
public static bool GetIsWMEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsWMEnabled);
}
public static void SetIsWMEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsWMEnabled, value);
}
private static void OnWMTextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox txtbox = sender as TextBox;
if (txtbox != null)
{
txtbox.Text = (string) e.NewValue;
}
}
private static void OnIsWMEnabled(object sender, DependencyPropertyChangedEventArgs e)
{
TextBox txtbox = sender as TextBox;
if (txtbox != null)
{
bool isEnabled = (bool)e.NewValue;
if (isEnabled)
{
txtbox.GotFocus += OnInputTextBoxGotFocus;
txtbox.LostFocus += OnInputTextBoxLostFocus;
}
else
{
txtbox.GotFocus -= OnInputTextBoxGotFocus;
txtbox.LostFocus -= OnInputTextBoxLostFocus;
}
}
}
private static void OnInputTextBoxLostFocus(object sender, RoutedEventArgs e)
{
var varTxtbox = e.OriginalSource as TextBox;
if (varTxtbox != null)
{
if (string.IsNullOrEmpty(varTxtbox.Text))
varTxtbox.Text = GetWMText(varTxtbox);
}
}
private static void OnInputTextBoxGotFocus(object sender, RoutedEventArgs e)
{
var varTxtbox = e.OriginalSource as TextBox;
if (varTxtbox != null)
{
if (varTxtbox.Text == GetWMText(varTxtbox))
varTxtbox.Text = string.Empty;
}
}
}
}
Now add the reference of this class to the xaml file and see the xaml file code as below:
<Window x:Class="WaterMark.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:wm="clr-namespace:WM"
>
<Grid>
<TextBox x:Name="txtFirstName" wm:WaterMark.IsWMEnabled="true"
wm:WaterMark.WMText="FirstName" Height="20" Margin="52,87,335,204"></TextBox>
<TextBox x:Name="txtLastName" wm:WaterMark.IsWMEnabled="true"
wm:WaterMark.WMText="LastName" Height="20" Margin="52,138,335,153"></TextBox>
<Button x:Name="btnSubmit" Height="25" Content="Submit" Margin="52,189,401,97" />
</Grid>
</Window>
Now running the application the screen will be look like:
As you can see in the textbox watermark is showing. After putting the FirstName window will look like this: