1

I'm creating a login page using visual studio 2012 for windows 8 apps. I'm adding a username and password box, as expected, but instead of putting text beside the boxes to show what each is for, I want to write it within the box, as done in this picture: http://www.windows8designhandbook.com/img/gallery/nasdaq/nasdaq_splash_login.png

How do I get the default text in there? I'm using a textbox for the userID and a passwordbox for the password.

JDB
  • 25,172
  • 5
  • 72
  • 123
  • 1
    And what type of project are you building for Windows 8? Silverlight or HTML? Windows application? – Mike Perrenoud Jun 13 '13 at 17:07
  • Windows Application. I'm working with the .xaml files – Tolu Kalejaiye Jun 13 '13 at 17:13
  • If its a XAML application, the answer was given in this post: [Make “default” text to appear in an empty TextBox without focus using XAML][1] [1]: http://stackoverflow.com/questions/10428230/make-default-text-to-appear-in-an-empty-textbox-without-focus-using-xaml – Richard Seal Jun 13 '13 at 17:18
  • 1
    @RichardSeal But that solution uses a text box. I was hoping to be able to do it with a password box. – Tolu Kalejaiye Jun 13 '13 at 17:21
  • Yes, but the principles should still apply. You are still trying to create a watermark. – Richard Seal Jun 13 '13 at 17:24
  • Please post answers as answers (do not insert answers into your question... it is confusing for other users in the long run) – JDB Jun 13 '13 at 17:36
  • @Richard - Unfortunately, the principle does not apply to password textboxes as they will obfuscate the prompt text (***). You must modify the behavior or visual tree of the password box. The article the OP found is more appropriate then the linked Q/A. – JDB Jun 13 '13 at 18:03
  • Tolu - The last link in the accepted answer of @Michael's duplicate question should cover you, and seems to be a lot easier then the example you found. – JDB Jun 13 '13 at 18:08

1 Answers1

0

The OP included the following in an update to his question:

I think I've found an answer: http://julmar.com/blog/mark/?p=300

The linked article contains the following summary in the opening paragraph:

In the previous post, I wrote about a Blend behavior for Windows Store apps to add a watermark to a TextBox. The next question I got was “Well, what about a PasswordBox?” PasswordBox is a bit tricker since it doesn’t allow text to be displayed in the clear – so our little trick of changing the Text property doesn’t work here. So, instead, let’s get a little hacky (or clever depending on how you look at it I suppose). We can use the same series of events (GotFocus/LostFocus/Loaded) but instead of changing text, let’s add a new TextBlock into the visual tree of the PasswordBox to display our watermark text.

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

A similar question, Create WPF Watermark in Pure XAML, contains a link to a Code Project article, A WatermarkTextBox in 3 lines of XAML, which proposes a solution that should work equally well for a regular textbox or a password textbox:

<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>
Community
  • 1
  • 1
JDB
  • 25,172
  • 5
  • 72
  • 123