Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » WPF » Watermark TextBox in WPF

Watermark TextBox in WPF

In this article I am going to show how we can use watermark textbox in WPF application.

Author Rank :
Page Views : 7739
Downloads : 270
Rating :
 Rate it
Level : Advanced
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
WaterMarkTextBox.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


In this article I am going to show how we can use watermark textbox in WPF application.

For this I have added a class file named as Helper.cs.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Data;

using System.Windows;

 

namespace WaterMarkTextBox

{

    class Helper : IMultiValueConverter

    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            if (values[0] is bool && values[1] is bool)

            {

                bool hasText = !(bool)values[0];

                bool hasFocus = (bool)values[1];

 

                if (hasFocus || hasText)

                    return Visibility.Collapsed;

            }  

            return Visibility.Visible;  

        }  

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)

        {

            throw new NotImplementedException();

        }

    }

}

 

This is my XAML code..

<Window x:Class="WaterMarkTextBox.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WaterMark TextBox" Height="350" Width="525"

       xmlns:local="clr-namespace:WaterMarkTextBox">

 

    <Window.Resources>

        <SolidColorBrush x:Key="brushWatermarkBackground" Color="White" />      

        <SolidColorBrush x:Key="brushWatermarkBorder" Color="Indigo" />

        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />

        <local:Helper x:Key="Helper" />

        <Style x:Key="EntryFieldStyle" TargetType="Grid" >

            <Setter Property="HorizontalAlignment" Value="Stretch" />

            <Setter Property="VerticalAlignment" Value="Center" />

            <Setter Property="Margin" Value="20,0" />

        </Style>

    </Window.Resources>

    <Grid x:Name="LayoutRoot">

        <Grid.RowDefinitions>

            <RowDefinition Height="30" />           

            <RowDefinition Height="30" />

            <RowDefinition Height="10" />

            <RowDefinition Height="30" />

            <RowDefinition Height="10" />

            <RowDefinition Height="30" />

            <RowDefinition Height="10" />

            <RowDefinition Height="30" />

            <RowDefinition Height="10" />

            <RowDefinition Height="30" />

            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="100"></ColumnDefinition>

            <ColumnDefinition Width="100"></ColumnDefinition>

            <ColumnDefinition Width="Auto"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid Grid.Row="1" Grid.Column="1">

            <TextBlock x:Name="tbFirstName">First Name</TextBlock>

        </Grid>

        <Grid Grid.Row="1" Grid.Column="2">

            <TextBlock Margin="5,2" Text="First Name ..." Foreground="LightGray">

                <TextBlock.Visibility>

                    <MultiBinding Converter="{StaticResource Helper}">

                        <Binding ElementName="txtFirstName" Path="Text.IsEmpty" />

                        <Binding ElementName="txtFirstName" Path="IsFocused" />

                    </MultiBinding>

                    </TextBlock.Visibility>

            </TextBlock>

            <TextBox Name="txtFirstName" Background="Transparent"

                     Width="140" MaxLength="50" />

        </Grid>       

        <Grid Grid.Row="3" Grid.Column="1">

            <TextBlock x:Name="tbLastName">Last Name</TextBlock>

        </Grid>

        <Grid Grid.Row="3" Grid.Column="2">

            <TextBlock Margin="5,2" Text="Last Name ..." Foreground="LightGray" >

                <TextBlock.Visibility>

                    <MultiBinding Converter="{StaticResource Helper}">

                        <Binding ElementName="txtLastName" Path="Text.IsEmpty" />

                        <Binding ElementName="txtLastName" Path="IsFocused" />

                    </MultiBinding>

                    </TextBlock.Visibility>

            </TextBlock>

            <TextBox Name="txtLastName" Background="Transparent" Width="140" MaxLength="50" />

        </Grid>

        <Grid Grid.Row="5" Grid.Column="1">

            <TextBlock x:Name="tbEmail">Email</TextBlock>

        </Grid>

        <Grid Grid.Row="5" Grid.Column="2">

            <TextBlock Margin="5,2" Text="Email..." Foreground="LightGray">

                <TextBlock.Visibility>

                    <MultiBinding Converter="{StaticResource Helper}">

                        <Binding ElementName="txtEmail" Path="Text.IsEmpty" />

                        <Binding ElementName="txtEmail" Path="IsFocused" />

                    </MultiBinding>

                    </TextBlock.Visibility>

            </TextBlock>

            <TextBox Name="txtEmail" Background="Transparent"

                     Width="140" MaxLength="50" />

        </Grid>

        <Grid Grid.Row="7" Grid.Column="1">

            <TextBlock x:Name="tbCountry">Country</TextBlock>

        </Grid>

        <Grid Grid.Row="7" Grid.Column="2">

            <TextBlock Margin="5,2" Text="Country..." Foreground="LightGray">

                <TextBlock.Visibility>

                    <MultiBinding Converter="{StaticResource Helper}">

                        <Binding ElementName="txtCountry" Path="Text.IsEmpty" />

                        <Binding ElementName="txtCountry" Path="IsFocused" />

                    </MultiBinding>

                    </TextBlock.Visibility>

            </TextBlock>

            <TextBox Name="txtCountry" Background="Transparent"

                     Width="140" MaxLength="50" />

        </Grid>        

        <Grid Grid.Row="9" Grid.Column="2">

            <Button x:Name="Submit" Content="Submit"></Button>

        </Grid>       

    </Grid>

</Window>

 

When we run the application then window will look like this.


TextBoxWaterMark1.JPG

Image 1.

If we write in textbox then

TextBoxWaterMark2.JPG

Image 2.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Watermark TextBox in WPF by formula On June 1, 2010
Am not able to run this program due to :
1) Assembly " watermarkTextBox" not found.... and
2) The type "local-helper"not found....
I dnt knw hw to rectify this.. can anyone plz suggest..am using vs2008...
Reply | Email | Modify 
Re: Watermark TextBox in WPF by Dorababu On March 7, 2011
Same error for me too can any one help
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.