Home ? Source Code ? XAML only WPF Watermarked TextBox

XAML only WPF Watermarked TextBox

2015-03-06 01:54:28
The author
Download(s): 1
Point (s): 1?
Category Category:


Translated by [email protected] Author:[email protected]
A lot of the examples I found on how to add a watermark to a TextBox involve a significant amount of code-behind, handling text changed events, or making use of a customIValueConverterorIMultiValueConverter. In this article, I'll show how to add a watermark to a TextBox by only using XAML markup.
A demonstration project I recently worked on was pre-setting the text of a textbox as kind of a poor-man's watermark. But when you started typing in the box, you have to first clear away the default value. That's not a watermark, that's just annoying. I wanted to fix this, but I didn't want to write a bunch of C# code to make it happen. I knew it could be done in XAML.
Using the code
Let's just jump right to it


First we wrap everthing in aGrid. We'll use this as a container to define our overall layout. Then we add ourTextBoxfor user input, and ourTextBlockas our watermark. Since we don't specify row and column definitions, these controls will overlap - and we want that; a watermark should visually appear on top of a textbox. On our TextBlock however, we don't want it be included in any hit test results, so we'll set the IsHitTestVisible property to false.
For theTextBlock's style, we'll default it to collapsed, and then set it to visible when our DataContext's Text property does not have content AND when the TextBox does not have keyboard focus.
And that's it! No converters, no extensions, just pure XAML.

Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
WatermarkExample.sln1,017.00 B2015-03-04 22:11
01.97 kB
App.config189.00 B2015-03-04 22:11
App.xaml587.00 B2015-03-04 22:11
App.xaml.cs349.00 B2015-03-04 22:11
MainWindow.xaml2.18 kB2015-03-04 22:11
MainWindow.xaml.cs682.00 B2015-03-04 22:11
packages.config296.00 B2015-03-04 22:11
01.97 kB
AssemblyInfo.cs2.24 kB2015-03-04 22:11
Resources.Designer.cs2.79 kB2015-03-04 22:11
Resources.resx5.48 kB2015-03-04 22:11
Settings.Designer.cs1.08 kB2015-03-04 22:11
Settings.settings201.00 B2015-03-04 22:11
01.97 kB
MainViewModel.cs1.60 kB2015-03-04 22:11
ViewModelLocator.cs1.36 kB2015-03-04 22:11
WatermarkExample.csproj5.53 kB2015-03-04 22:11
Sponsored links


(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words




  • 1
  • Page 1
  • Total 1

XAML only WPF Watermarked TextBox (11.36 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points


Don't have an account? Register now
Need any help?
Mail to: [email protected]


CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D
全民欢乐捕鱼攻略技巧 北京pk10助手下载 玩三公有什么技巧赢钱 两万开什么店稳赚不赔 开奖公告快乐时时10分钟赚10万 时时彩最快开奖 内蒙古时时开奖号码 2019时时彩20分钟开奖 猜大小单双技巧微信 重庆彩开奖号码官方 老时时自由的百科 1.992高赔率奖金pk10app pk10怎么抓走势 双色球19103蓝号 最老版单机斗地主 赌博押牛牛做庄的技巧 百人棋牌皇家炸金花