The WatermarkTextBox control is a custom user control for C# WPF .NET Core v6.0 that displays a watermark until the user starts typing. The control is often used for providing hints or default values for input fields. It offers a user-friendly and visually appealing way to interact with text inputs in your application.
- Display a watermark text when the TextBox is empty
- Remove the watermark when the TextBox receives focus or the user starts typing
- Reapply the watermark if the TextBox loses focus and is still empty
- Allow customization of the watermark text and color
- Allow customization of the foreground and background colors of the TextBox
- Implement data binding for the TextBox value
To use the WatermarkTextBox Control in your WPF application, follow these steps:
Clone or download the source code from GitHub and add the WatermarkTextBox.xaml and WatermarkTextBox.xaml.cs files to your project.
In the XAML file where you want to use the control, add the following namespace declaration at the top:
xmlns:controls="clr-namespace:YourNamespace.Controls;assembly=YourAssemblyName"
Replace YourNamespace
with the actual namespace of the WatermarkTextBox Control in your project and YourAssemblyName
with the actual name of your assembly.
Add the WatermarkTextBox Control to your XAML layout:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your text here" />
Bind the Text
property of the WatermarkTextBox Control to a property in your ViewModel:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your text here" Text="{Binding MyTextProperty, Mode=TwoWay}" />
The WatermarkTextBox Control has the following dependency properties:
Watermark
(string): The text to display as the watermark when the TextBox is empty. Default value: "Watermark".WatermarkColor
(Brush): The color of the watermark text. Default value: LightGray.Text
(string): The text content of the TextBox. Default value: "".
To customize the watermark text and color, set the Watermark
and WatermarkColor
properties in XAML:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your email" WatermarkColor="Red" />
You can customize the appearance of the WatermarkTextBox Control by modifying the XAML template in the WatermarkTextBox.xaml
file. For example, you can change the foreground color, background color, and border style of the TextBox.
Add the WatermarkTextBox Control to your XAML layout with default watermark text:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" />
Add the WatermarkTextBox Control to your XAML layout with custom watermark text:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your email" />
Add the WatermarkTextBox Add the WatermarkTextBox Control to your XAML layout with custom watermark text and color:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your email" WatermarkColor="Red" />
Bind the Text
property of the WatermarkTextBox Control to a property in your ViewModel:
<controls:WatermarkTextBox x:Name="MyWatermarkTextBox" Watermark="Enter your email" Text="{Binding MyEmailProperty, Mode=TwoWay}" />
If you'd like to contribute to the development of the WatermarkTextBox Control, please follow these guidelines:
- Fork the repository on GitHub.
- Create a branch for your changes.
- Make your changes and commit them to your branch.
- Create a pull request with a description of your changes.
All contributions are greatly appreciated!
If you encounter any issues or have questions about the WatermarkTextBox Control, please create an issue on the GitHub repository, and we will do our best to help you.
WatermarkTextBox Control was developed by Jon M. Sales as an open-source project. Special thanks to all contributors and users of the control.
Happy coding!