我正在寻找一种方法来将占位符文本添加到文本框中,就像在html5中使用文本框一样.
即如果文本框没有文本,则添加文本Enter some text here,当用户点击它时,占位符文本消失并允许用户输入自己的文本,如果文本框失去焦点但仍然没有文本,则占位符为添加回文本框.
例如,当文本框为空时,Facebook在"搜索"文本框中有"搜索"提示文本.
如何用WPF文本框实现这一点?

任何人都可以指向一个基本的Windows窗体TextBox的良好实现,它最初会显示水印文本,当光标进入时它会消失吗?我想我可以通过创造性地使用Enter和Leave事件来创建我自己的东西,但我确信在某个地方有一个完全可用的实现.我看到了WPF实现,如果有必要,我可以嵌套它,但原生的WinForms TextBox派生会更好.
到目前为止,我有这个; 尚未尝试过,但有没有人看到任何明显的问题?
public class WatermarkTextBox:TextBox
{
public string WatermarkText { get; set; }
public Color WatermarkColor { get; set; }
private Color TextColor { get; set; }
private bool isInTransition;
public WatermarkTextBox()
{
WatermarkColor = SystemColors.GrayText;
}
private bool HasText { get { return Text.IsNotNullOrBlankOr(WatermarkText); }}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
if (HasText) return;
isInTransition = true;
ForeColor = TextColor;
Text = String.Empty;
isInTransition = false;
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
if (!isInTransition) …Run Code Online (Sandbox Code Playgroud) 我希望在TextBox用户没有键入任何内容并且TextBox处于空闲状态时显示占位符文本.
在Andriod中,可以使用 android:hint="some Text"
In iPhone完成它textFild.placeholder = "some text";
我如何在Windows 8 metro应用程序中执行此操作?
谢谢
System.Windows.Forms.TextBox使用C#在.Net 2.0中实现水印功能的最佳方法是什么?
编辑:
使用CodeProject中的现成组件非常简单.它还带有代码项目开放许可证(CPOL).
激活应用程序时,将出现带有文本"hello"的文本框.
我的问题是:
当你点击文本框以输入数据时,我想在XAML代码中自动删除文本,我该怎么办?
我无法理解IsEmpty这段代码(Path=Text.IsEmpty)(来自水印TextBox)的来源:
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}"
Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="Type to search ..." Foreground="Gray"
Visibility="{Binding ElementName=entry, Path=Text.IsEmpty,
Converter={StaticResource BooleanToVisibilityConverter}}"/>
<TextBox Name="entry" Background="Transparent"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
你可以看到一个字符串没有任何IsEmpty属性.A DependencyProperty也没有任何IsEmpty成员.我甚至尝试在IsEmpty对象浏览器窗口中搜索,但没有任何相关结果解释代码.
你能解释一下IsEmpty这里的参考吗?(关于它的任何参考链接都很棒).
谢谢!
我正在使用Watermark文本框,如WPF中的Watermark TextBox
<Grid Grid.Row="0" Background="{StaticResource brushWatermarkBackground}" Style="{StaticResource EntryFieldStyle}" >
<TextBlock Margin="5,2" Text="This prompt dissappears as you type..." Foreground="{StaticResource brushWatermarkForeground}"
Visibility="{Binding ElementName=txtUserEntry, Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}" />
<TextBox Name="txtUserEntry" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
如何将此应用于PasswordBox?
在WPF TextBox中,我想提供在第一次输入后消失的描述.这应该为用户提供一些帮助,他应该在文本字段中输入什么.
可以使用Label在给定的TextBox上方显示文本.我对一个例子感兴趣,但找不到任何例子.理想情况下,解决方案仅适用于XAML.
这是用于登录屏幕上的TextBox控件,其中TextBox包含用户名.我希望TextBox以下列方式执行:
当内容为空时,内容应设置为"用户名".
单击TextBox时,我希望将内容设置为""即; 没有(除非内容已经由用户编辑).
这是一个非常标准的功能,就像这个wordpress登录(在页面顶部).我不敢想到比这更好的例子我害怕:)
所以,无论如何,我已经使用ViewModel做了这个并且它运行良好,但是我想知道这是否可以完全来自XAML端.没有业务逻辑,所以我认为没有VM就更好.
嘿,我尝试实施John制作Watermark的课程.
我被困住了,想知道是否有人可以帮助我....在wpf中添加了提到的2个类:
<AdornerDecorator>
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,184,664,0" x:Name="cbVideoDevices" VerticalAlignment="Top" Width="316" Initialized="cbVideoDevices_Initialized" SelectionChanged="cbVideoDevices_SelectionChanged">
<Controls:WatermarkService.Watermark>
<TextBlock>Type here to search text</TextBlock>
</Controls:WatermarkService.Watermark>
</ComboBox>
</AdornerDecorator>
Run Code Online (Sandbox Code Playgroud)
无论我尝试什么,我都会因控制不存在而不断出现错误,或者财产不会退出.我在他的课程中没有错误,所以我认为引用很好,但在我看来System.Windows.Control丢失了....但我找不到它添加它...
任何帮助,非常感谢.
编辑:在Liz的帮助下,我让这个工作,但让任何人都知道,谁使用这个.
如何制作它,以便在单击文本框时,文本框中最初的文本("在此处输入文本")将被清除,并且仅在第一次单击时被清除?
编辑:对不起,我正在使用C#.
我构建了一个小的 WPF TextBox 来检查它的内容是否有效。现在我想实现提供建议的可能性。但不像互联网上的示例那样弹出一个带有建议的列表。我正在寻找一个示例,它可以像这样选择 TextBox:
c# ×9
wpf ×9
.net ×6
textbox ×4
watermark ×4
xaml ×4
winforms ×2
.net-2.0 ×1
.net-4.5 ×1
autocomplete ×1
mvvm ×1
passwordbox ×1
placeholder ×1
windows ×1
windows-8 ×1