How can I put some text into a textbox which is removed automatically when user types something in it? (In WPF)
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
@Veton - I really like the simplicity of your solution but my reputation isn't high enough to bump you yet.
@Tim Murphy - That "Two-way binding requires Path or XPath" error was an easy fix... updated code including some other little tweaks (only WPF tested):
If rather than having the watermark's visibility depend on the control's focus state, you want it to depend on whether the user has entered any text, you can update John Myczek's answer (from
OnWatermarkChanged
down) toThis makes more sense if your textbox gets focus automatically when displaying the form, or when databinding to the Text property.
Also if your watermark is always just a string, and you need the style of the watermark to match the style of the textbox, then in the Adorner do:
Well here is mine: not necessarily the best, but as it is simple it is easy to edit to your taste.
The converter, as it is written now it is not necessary that it is a MultiConverter, but in this wasy it can be extended easily
and finally the code behind:
Here is the simplest solution:
This is a textbox with transparent backgound overlaying a label. The label's gray text is turned transparent by a data trigger that fires whenever the bound text is something other than empty string.
I decided to solve this via a Behavior. It uses a
Hint
property to define the text to display (could also be an object, if you prefer) and aValue
property to evaluate wether the hint should be visible or not.The Behavior is declared as follows:
It wraps the target with it's own template, adding to it a label:
To use it, just add it as a behavior and bind your values (in my case I add it in a ControlTemplate, hence the binding):
I would love feedback if this is considered a clean solution. It does not require static dictionaries and hence has no memory leak.
hi i put this task into a behavior. so you just have to add somthing like this to your textbox
you can find my blog post here