I want to implement something exactly like "Changing the Default Text in the Search Box" from http://msdn.microsoft.com/en-us/library/aa905322.aspx for a WPF search TextBox. The box should show some greyed out "Search.." text when it's empty, and then function normally when typed in. The linked article shows how to do this in javascript. How would one start down this path in WPF? The best idea I've had so far is another text box on top of the main one that goes invisible whenever the search textbox gets focus or text.
-
You could transform the textbox to have gray text whenever its empty and a variable that would tell you that is empty, so that when you clicked Search it would not go searching for "Search..."
Or you could use something similar to what you are saying, but instead of a textbox above you could have text below. If on top you have a textbox with transparent background and on the bottom you have a label that has "Search" when the top textbox is empty that should solve the problem.
-
As always in WPF, there are many ways to achieve your goal.
Perhaps the cleanest way is to subclass
TextBoxand add a new property calledHintText. The template for your control would displayHintText(probably in italics and gray) as long asTextis empty (""). Otherwise, it would display theTextjust like a regularTextBox.An alternative that doesn't involve writing your own control is to re-template
TextBoxand use theTagproperty to store the hint text.Another alternative is to write a
UserControlthat combines aTextBoxwith, say, aTextBlockinside the sameGrid. TheTextBlockwould contain the hint text and would only be displayed if theTextBox'sTextis empty. This is probably the easiest to achieve, but is also the least flexible.HTH, Kent
-
Try the InfoTextBox sample from Kevin Moore's Bag-o-Tricks. You can download it from http://work.j832.com/2008/01/real-update-to-bag-o-tricks.html
-
The best way I think for these types of things is to set the background using a visual brush. The visual brush lets you paint a background using Visual Elements, combine it with a trigger based on text being empty and it's done.
Example for Empty List Box message is here, basically the same thing. http://adammills.wordpress.com/2010/08/04/simple-empty-template-for-itemscontrols/
0 comments:
Post a Comment