C# 使用 XAML 使“默认”文本显示在没有焦点的空文本框中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10428230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make "default" text to appear in an empty TextBox without focus using XAML
提问by Arsen Zahray
I want to create a TextBox, which would have a gray "default" text appear in it, if it's
我想创建一个文本框,它会出现一个灰色的“默认”文本,如果它是
a) empty
a) 空的
b) has lost focus
b) 失去焦点
when the user enters the text box, the gray "default" text should dissappear.
当用户输入文本框时,灰色的“默认”文本应该消失。
I've tried to do this using ControlTemplate.Triggers, but I can't seem to find HasFocusproperty.
我尝试使用 来做到这一点ControlTemplate.Triggers,但我似乎无法找到HasFocus属性。
What is the best way to do this using XAML?
使用 XAML 执行此操作的最佳方法是什么?
采纳答案by Steve Greatrex
Whilst there is no real benefit in re-inventing the wheel, it might be interesting to see how this can be done. The easiest way to do this (in pure XAML) is to create a ControlTemplatefor the TextBoxthat overlays a TextBlockwhen it is not focussed and does not contain text:
虽然重新发明轮子没有真正的好处,但看看如何做到这一点可能会很有趣。执行此操作的最简单方法(在纯 XAML 中)是在未聚焦且不包含文本时为覆盖 a创建一个ControlTemplatefor :TextBoxTextBlock
<ControlTemplate TargetType="TextBox">
<Grid>
<TextBox Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Your Prompt Here"
Margin="5,0,5,0"
Foreground="#FF808080"
FontStyle="Italic"
IsHitTestVisible="False"
x:Name="UserMessage"
Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Text" Value=""/>
<Condition Property="IsKeyboardFocusWithin" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="UserMessage" Value="Visible"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
The MultiTriggermeans "set Visibility to Visible if the Textproperty is empty AND the TextBox does not have keyboard focus"
该MultiTrigger手段“显示设置更改为可见,如果Text属性是空的,文本框没有键盘焦点”
If you want to make this more reusable then you could create a custom control with this as it's default template and with a Dependency Property containing the prompt message
如果您想让它更可重用,那么您可以使用它作为默认模板创建一个自定义控件,并使用包含提示消息的依赖属性
回答by eandersson
You could simply use the Extended WPF Toolkit's WatermarkTextBox.

您可以简单地使用扩展 WPF 工具包的WatermarkTextBox。

I wrote a small guide based on your comment on how you to add and use the library in your project.
我根据您对如何在项目中添加和使用库的评论编写了一个小指南。
Step 1)Right-click on Referencesin your project and choose Add Reference.
步骤 1)References在您的项目中右键单击并选择Add Reference.


Step 2)Locate and add the dll file WPFToolkit.Extended.dll.
步骤 2)找到并添加 dll 文件WPFToolkit.Extended.dll。


Step 3)Last you need to add the XAMLcode.
步骤 3)最后,您需要添加XAML代码。
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<extToolkit:WatermarkTextBox Watermark="Enter First Name" />
</Grid>
</Window>
The key here is to add the referenceto the dll file.
这里的关键是添加reference到dll文件中。
xmlns:extToolkit="http://schemas.xceed.com/wpf/xaml/toolkit"
Then you can simply use it in XAMLlike this.
然后你可以XAML像这样简单地使用它。
<extToolkit:WatermarkTextBox Watermark="Enter First Name" />

