【问题标题】:Putting default text in a password box in visual studio [duplicate]在Visual Studio的密码框中放置默认文本[重复]
【发布时间】:2013-06-14 02:17:26
【问题描述】:

我正在使用 Visual Studio 2012 for Windows 8 应用程序创建登录页面。正如预期的那样,我添加了一个用户名和密码框,但我不想在框旁边放置文本以显示每个框的用途,而是将其写在框中,如下图所示:http://www.windows8designhandbook.com/img/gallery/nasdaq/nasdaq_splash_login.png

如何在其中获取默认文本?我使用文本框作为用户 ID,使用密码框作为密码。

【问题讨论】:

  • 您正在为 Windows 8 构建什么类型的项目? Silverlight 还是 HTML? Windows 应用程序?
  • Windows 应用程序。我正在使用 .xaml 文件
  • 如果它是一个 XAML 应用程序,答案在这篇文章中给出:[使用 XAML 使“默认”文本出现在没有焦点的空文本框中][1] [1]:stackoverflow.com/questions/10428230/…
  • @RichardSeal 但该解决方案使用文本框。我希望能够使用密码框来做到这一点。
  • 是的,但原则仍应适用。您仍在尝试创建水印。

标签: c# visual-studio-2012 windows-8 passwordbox


【解决方案1】:

OP 在他的问题的更新中包含以下内容:

我想我找到了答案:http://julmar.com/blog/mark/?p=300

链接的文章在开头段落中包含以下摘要:

在上一篇文章中,我写了关于 Windows 应用商店应用程序的 Blend 行为以将水印添加到 TextBox。我得到的下一个问题是“嗯,PasswordBox 怎么样?” PasswordBox 有点小技巧,因为它不允许明文显示文本——所以我们更改 Text 属性的小技巧在这里不起作用。所以,相反,让我们变得有点 hacky(或者聪明,取决于你如何看待它,我想)。我们可以使用相同的事件系列(GotFocus/LostFocus/Loaded),但不是更改文本,而是在 PasswordBox 的可视化树中添加一个新的 TextBlock 来显示我们的水印文本。

文章Adding a watermark to a PasswordBox in a Windows Store app
博客Wandering through the wilderness: Exploring the world of .NET 4.0, iOS, WPF, MVVM and other cool technologies

一个类似的问题Create WPF Watermark in Pure XAML 包含指向代码项目文章A WatermarkTextBox in 3 lines of XAML 的链接,该文章提出了一个同样适用于常规文本框或密码文本框的解决方案:

<Grid Background="{StaticResource brushWatermarkBackground}">
    <TextBlock Margin="5,2"
               Text="Prompt..."
               Foreground="{StaticResource brushForeground}"
               Visibility="{Binding ElementName=txtUserEntry, 
                                    Path=Text.IsEmpty,
                                    Converter={StaticResource
                                        BooleanToVisibilityConverter}}" />
    <TextBox Name="txtUserEntry"
             Background="Transparent"
             BorderBrush="{StaticResource brushBorder}" />
</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 2011-12-20
    • 1970-01-01
    • 2015-01-20
    • 2011-05-27
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多