Watermark TextBox

Searching on the Internet about creating a watermark TextBox, there are many ways to achieve but finally I do it on my own.

<Grid Width="182" Margin="2">
    <Rectangle Fill="White"/>
    <TextBlock Margin="4,3" Text="Search..." FontFamily="Segoe UI"
    Foreground="#FFC0C0C0" FontSize="20" FontStyle="Italic"
    Visibility="{Binding ElementName=txtSearchBox, Path=Text.IsEmpty,
    Converter={StaticResource booleanToVisibilityConverter}}"/>
    <TextBox x:Name="txtSearchBox" BorderThickness="0" Margin="0,3"
    FontSize="20" FontFamily="Segoe UI Light" Background="Transparent"  />
</Grid>


With the above xaml code, you need BooleanToVisibilityConverter, simply put it inside Resources.

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
</Window.Resources>

It’s done up to here. For customizing the width and height, do it in the outermost Grid, and also change the FontSize for both TextBlock and TextBox so that they are in sync 🙂

Result:

WatermarkTextBox

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s