13 Replies - 5043 Views - Last Post: 07 June 2012 - 04:38 PM
#1
How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 08:51 AM
I've been able to create a custom class that extends TextBox, but I am pretty sure that this can be done through xaml.
Replies To: How to create "placeholder" text in a textbox in xaml
#2
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 08:55 AM
http://c-sharp-snipp...older-text.html
#3
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 09:14 AM
#4
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 11:01 AM
Bind the textbox to a property, then set the default text of the property.
later when someone enters new text - that becomes the value of the property.
#5
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 12:17 PM
EDIT: Fixed the MSDN link
This post has been edited by h4nnib4l: 07 June 2012 - 01:41 PM
#6
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 12:30 PM
It sounds like you are describing a Watermarked Text Box. If that's the case, I suggest the second answer on this thread:
http://stackoverflow...-textbox-in-wpf
Not the accepted one, but the one with many more votes.
#7
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 12:44 PM
Curtis Rutland, on 07 June 2012 - 12:30 PM, said:
http://stackoverflow...-textbox-in-wpf
Not the accepted one, but the one with many more votes.
I like that one too, but it doesn't answer the OP's question where he wants to implement it fully in XAML. The WatermarkService there is XAML + code. If he was going to do XAML + code, then there is the extended Textbox that he already has.
#8
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 12:49 PM
#9
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 01:00 PM
OK. CHEAT.
Tell the XAML you are binding to a property that doesn't exist, then set your placekeeper text in the FallbackValue of the property
<TextBox Height="154" HorizontalAlignment="Left" Margin="44,40,0,0" Name="textBox1" VerticalAlignment="Top" Width="317"
Text="{Binding Path=dummyproperty, FallbackValue='THis is a bunch of placekeeper text'}"/>
#10
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 01:02 PM
tlhIn`toq, on 07 June 2012 - 11:01 AM, said:
Bind the textbox to a property, then set the default text of the property.
later when someone enters new text - that becomes the value of the property.
I think I went down this route before. I liked the simplicity and elegance of it because I needed to bind the input from the text box anyway.
It had issues though:
- It doesn't have the stylized faded text or whatever other style I wanted to use to denote that it was just a prompt.
- It makes handling required field difficult.
- Mismatching types
I can live without the pretty UI that I listed as the first issue, but the second issue was kind of a pain. It was relatively easy to distinguish the default text "Phone Number" from a valid entered phone number. I had to do some trickery with my Validator as I recall including breaking some encapsulation: my Validator ended up knowing what the default text was. "Weight" was kind of fun because I wanted to bind my text field to a numeric property, but now I needed to support text. I think with judicious use of TypeConverters I could have made things bind with a Decimal, but I figured since the ViewModel was supposed to mediate between the View and Model, it may as well mediate and return invalid or missing weight as the default text. Things got harder to distinguish when it came to "Address" (as well as other more free form fields like "Comments"). Yes, it is a really low probability that somebody's address will be "Address", but what if their address really is "Address"?
This post has been edited by Skydiver: 07 June 2012 - 01:02 PM
#11
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 01:40 PM
#12
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 02:26 PM
tlhIn`toq, on 07 June 2012 - 01:00 PM, said:
OK. CHEAT.
Tell the XAML you are binding to a property that doesn't exist, then set your placekeeper text in the FallbackValue of the property
<TextBox Height="154" HorizontalAlignment="Left" Margin="44,40,0,0" Name="textBox1" VerticalAlignment="Top" Width="317"
Text="{Binding Path=dummyproperty, FallbackValue='THis is a bunch of placekeeper text'}"/>
Is there a simple trick to getting it to bind to the real property when the user enters data? (And revert back to the dummy property when the user deletes all the data or fails validation?)
#13
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 02:52 PM
Skydiver, on 07 June 2012 - 03:26 PM, said:
Why? Just use the real property with the fallback value: That's what is for, to provide a value in the event there isn't one.
Beyond that its getting fairly complex. I'd do a lot of stuff in C# in the code behind, but that seems to not be acceptable.
#14
Re: How to create "placeholder" text in a textbox in xaml
Posted 07 June 2012 - 04:38 PM
tlhIn`toq, on 07 June 2012 - 02:52 PM, said:
Skydiver, on 07 June 2012 - 03:26 PM, said:
Why? Just use the real property with the fallback value: That's what is for, to provide a value in the event there isn't one.
Running the app shows the real value rather than the fallback value.
<StackPanel>
<TextBox Height="154" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="317"
Text="{Binding Path=Age, FallbackValue='THis is a bunch of placekeeper text'}"/>
<TextBox Height="154" HorizontalAlignment="Left" Name="textBox2" VerticalAlignment="Top" Width="317"
Text="{Binding Path=Nickname, FallbackValue='THis is a bunch of placekeeper text'}"/>
</StackPanel>
class ViewModel
{
public int Age { get; set; }
public string Nickname { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel() { Age = -1 };
}
}
Output is:

I also tried:
class ViewModel
{
public int? Age { get; set; }
public string Nickname { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
This just results in two empty TextBoxes.
tlhIn`toq, on 07 June 2012 - 02:52 PM, said:
Yeah, for the MVVM Zealots it is unacceptable. They barely accept that the pattern they espouse is actually MVVM+C where sometimes there is no getting around having a controller unless you want to pollute the Model, or the View, or the View Model with concepts that don't really belong there. Just like in the quick and dirty code above, the MainWindow constructor should not have known to instantiate a ViewModel. It should be whatever constructed the MainWindow that should tell it what view model to use. Purist say that a DI framework would take care of this. In that case, isn't the DI framework playing the part of a controller?
This post has been edited by Skydiver: 07 June 2012 - 04:48 PM
|
|

New Topic/Question
Reply




MultiQuote





|