文本框中的水印/提示/占位符文本?
如何将一些文本放入 TextBox
中,当用户在其中键入内容时,该文本将自动删除?
How can I put some text into a TextBox
which will be removed automatically when the user types something in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您可以创建一个水印,该水印可以添加到具有附加属性的任何
TextBox
中。 以下是附加属性的源代码:附加属性使用一个名为
WatermarkAdorner
的类,该源代码如下:现在您可以在任何 TextBox 上放置水印,如下所示:
水印可以是您想要的任何内容 (文本、图像...)。 除了适用于文本框之外,此水印还适用于组合框和项目控件。
此代码改编自 这篇博文。
You can create a watermark that can be added to any
TextBox
with an Attached Property. Here is the source for the Attached Property:The Attached Property uses a class called
WatermarkAdorner
, here is that source:Now you can put a watermark on any TextBox like this:
The watermark can be anything you want (text, images ...). In addition to working for TextBoxes, this watermark also works for ComboBoxes and ItemControls.
This code was adapted from this blog post.
仅使用 XAML,无扩展,无转换器:
Just using XAML, no extensions, no converters:
我不敢相信没有人发布来自 Xceed 的明显 扩展 WPF 工具包 - WatermarkTextBox 。 它工作得很好,并且是开源的,以防您想要定制。
编辑:虽然仍然开源,但该库现在仅免费用于非商业用途,请参阅 定价和许可证。
I can't believe that no one posted the obvious Extended WPF Toolkit - WatermarkTextBox from Xceed. It works quite well and is open source in case you want to customise.
Edit: though still open source, this library is only free for non-commercial use now, see pricing and license.
这是一个示例,演示如何在 WPF 中创建水印文本框:
TextInputToVisibilityConverter 定义为:
注意: 这不是我的代码。 我在这里找到了它,但我认为这个是最好的方法。
This is a sample which demonstrates how to create a watermark textbox in WPF:
TextInputToVisibilityConverter is defined as:
Note: This is not my code. I found it here, but I think this is the best approach.
有一篇关于 CodeProject 的文章如何在“3行XAML”中做到这一点。
好吧,它可能不是 3 行 XAML 格式,但它非常简单。
需要注意的一件事:
Text
上的IsEmpty
属性不是string
的属性,而是ICollectionView
的属性,并且可以显式设置为Path=Text.(componentModel:ICollectionView.IsEmpty)
(使用xmlns:componentModel="clr-namespace:System.ComponentModel; assembly=WindowsBase"
) 。 详细说明此处。There is an article on CodeProject on how to do it in "3 lines of XAML".
Ok, well it might not be 3 lines of XAML formatted, but it is pretty simple.
One thing to note: the
IsEmpty
property onText
is not a property ofstring
, but ofICollectionView
, and can be set explicitely asPath=Text.(componentModel:ICollectionView.IsEmpty)
(withxmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
). Detailed explanation here.使用样式的简单解决方案:
很好的解决方案:
https://code .msdn.microsoft.com/windowsdesktop/In-place-hit-messages-for-18db3a6c
Simple solution using style:
Great solution:
https://code.msdn.microsoft.com/windowsdesktop/In-place-hit-messages-for-18db3a6c
我看到了 John Myczek 的解决方案,及其关于兼容性的评论
ComboBox
和PasswordBox
,所以我改进了 John Myczek 的解决方案,如下:现在,
ComboBox
也可以Editable 和
PasswordBox
也可以添加水印。不要忘记使用上面 JoanComasFdz 的评论来解决边距问题。
当然,所有功劳都归于约翰·麦切克 (John Myczek)。
I saw John Myczek's solution, and its comments about Compatibility to
ComboBox
andPasswordBox
, so I improved John Myczek's solution, and here it is:Now, a
ComboBox
can be alsoEditable
, andPasswordBox
can add a watermark too.Don't forget to use JoanComasFdz's comment above to solve the margin problem.
And, of course, All the credit goes to John Myczek.
此库有水印。
Nuget 包
示例用法:
This library has a watermark.
Nuget package
Sample usage:
我创建了简单的纯代码实现,它也适用于 WPF 和 Silverlight:
用法:
I have created siple code-only implementation which works fine for WPF and Silverlight as well:
Usage:
我在使用 @john-myczek 的 代码 时遇到了一些困难绑定文本框。 由于文本框在更新时不会引发焦点事件,因此水印将在新文本下方保持可见。 为了解决这个问题,我只是添加了另一个事件处理程序:
I ran into a bit of difficulty when using @john-myczek's code with a bound TextBox. As the TextBox doesn't raise a focus event when it's updated, the watermark would remain visible underneath the new text. To fix this, I simply added another event handler:
TextBox水印
并添加文本框StaticResource样式的最简单方法
simplest Way to WaterMark Of TextBox
and add textbox StaticResource style
@Veton - 我真的很喜欢你的解决方案的简单性,但我的声誉还没有高到足以打败你。
@Tim Murphy - “双向绑定需要 Path 或 XPath”错误是一个简单的修复...更新的代码,包括一些其他的小调整(仅 WPF 测试):
@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):
您可以使用
GetFocus()
和LostFocus()
事件来执行此操作,示例如下:
you can use
GetFocus()
andLostFocus()
events to do thishere is the example:
这可以帮助检查您的代码。当应用于密码框时,它将显示密码,当用户键入时密码将消失。
This can help check it with your code.When applied to password box,it will show Password,which will disappear when usertypes.
MahApps.Metro for WPF 有一个内置水印控件(如果您不愿意)自己动手。 使用起来相当简单。
MahApps.Metro for WPF has a built-in watermark control, if you'd rather not roll your own. It's fairly straightforward to use.
设置带有柔和颜色占位符文本的文本框...
当文本框获得焦点时,清除它并更改文本颜色
Set up the text box with placeholder text in a soft color...
When the text box gets the focus, clear it and change the text color
我的解决方案非常简单。
在我的登录窗口中。 xaml是这样的。
代码是这样的。
只需决定隐藏或显示水印文本框就足够了。 虽然不漂亮,但是做工还是不错的。
My solution is quite simple.
In my login window. the xaml is like this.
the code is like this.
Just decide to hide or show the watermark textbox is enough. Though not beautiful,but work well.
这是我的:不一定是最好的,但因为它很简单,所以很容易根据您的口味进行编辑。
现在编写的转换器不一定是 MultiConverter,但在这个 wasy 中它可以轻松扩展
,最后是后面的代码:
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:
这是我的方法,非常适合 MVVM,我还检查文本框是否具有焦点,您也可以仅针对文本值使用常规触发器,重点是我只是在值更改时更改背景图像:
Here's my approach Is great for MVVM where I also check if the Text box has focus, you can also use a regular trigger just for the text value as well the point is I just change the background Image when value changes:
将 mahapps.metro 添加到您的项目中。
将包含上述代码的文本框添加到窗口中。
Add mahapps.metro to your project.
Add textbox with the above code to the window.
看另一个简单的解决方案:
我关注 GotFocus 和 LostFocus 事件。
XAML:
C#:
Look at another simple solotion:
I'm focused GotFocus and LostFocus events.
XAML:
C#:
这是最简单的解决方案:
这是一个透明背景覆盖标签的文本框。 标签的灰色文本通过数据触发器变为透明,只要绑定文本不是空字符串,该触发器就会触发。
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.
另请参阅此答案。 您可以使用 VisualBrush 和样式中的一些触发器更轻松地完成此操作:
为了提高此样式的可重用性,您还可以创建一组附加属性来控制实际的提示横幅文本、颜色、方向等。
Also, see this answer. You can accomplish this much more easily with a VisualBrush and some triggers in a Style:
To increase the re-usability of this Style, you can also create a set of attached properties to control the actual cue banner text, color, orientation etc.
这是 XAML 中的另一个简单解决方案:
XAML:
Here is another simple solution in XAML:
XAML:
嗨,我把这个任务变成了一种行为。 因此,您只需将类似的内容添加到文本框中
即可找到我的博客文章 此处
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
此技术使用“背景”属性来显示/隐藏占位符文本框。
当文本框具有焦点时显示占位符事件
工作原理:
这是基本示例。 出于我自己的目的,我将其转换为 UserControl。
这是用于检测 DataTrigger 中非空字符串的 ValueConverter。
This technique uses the Background property to show / hide placeholder textbox.
Placeholder is shown event when Textbox has the focus
How it works:
Here is basic example. For my own purposes I turned this into a UserControl.
Here is the ValueConverter to detect non-empty strings in the DataTrigger.
您可以为输入的文本保留一个单独的值,并且可以在“GotFocus”和“LostFocus”事件中将其与文本框的“文本”字段一起设置。 当您获得焦点时,如果没有值,您将需要清除文本框。 当您失去焦点时,您需要设置从文本框中获取“文本”值,然后将文本框的“文本”值重置为占位符(如果它为空)。
然后,您只需确保文本框的“文本”值初始化为占位符文本。
您可以进一步将其提取到扩展“TextBox”类的类中,然后在整个项目中重用它。
然后可以直接将其添加到 xaml 中。
You can keep a seperate value for the entered text and you can set it along with the "Text" field of the text box in the "GotFocus" and "LostFocus" events. When you get the focus, you'll want to clear the text box if there is no value. And when you loss the focus, you'll want to set the get the "Text" value from the text box and then reset the "Text" value of the text box to the place holder if it is empty.
Then you just have to make sure that the "Text" value of the text box is initialized to the place holder text.
You can further extract this into a class that extends the "TextBox" class and then reuse it through out your project.
And then this can be added in the directly in the xaml.
如果您希望水印的可见性不取决于控件的焦点状态,而是取决于用户是否输入了任何文本,则可以将 John Myczek 的答案(从
OnWatermarkChanged
向下)更新为This make more检测文本框在显示表单或数据绑定到 Text 属性时是否自动获得焦点。
另外,如果您的水印始终只是一个字符串,并且您需要水印的样式与文本框的样式相匹配,那么在装饰器中执行以下操作:
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:
我决定通过行为来解决这个问题。 它使用
Hint
属性来定义要显示的文本(如果您愿意,也可以是一个对象),并使用Value
属性来评估提示是否可见。行为声明如下:
它用自己的模板包装目标,并向其添加标签:
要使用它,只需将其添加为行为并绑定您的值(在我的例子中,我将其添加到 ControlTemplate 中,因此绑定):
如果这被认为是一个干净的解决方案,我希望得到反馈。 它不需要静态字典,因此没有内存泄漏。
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.