What's the simplest way to change this TextBox code to a watermark TextBox?
<TextBox Grid.Column="1" Name="txtBoxAddress" Width="200" GotKeyboardFocus="TxtBoxAddress_GotKeyboardFocus" Text="" KeyUp="TxtBoxAddress_KeyUp"></TextBox>
What's the simplest way to change this TextBox code to a watermark TextBox?
<TextBox Grid.Column="1" Name="txtBoxAddress" Width="200" GotKeyboardFocus="TxtBoxAddress_GotKeyboardFocus" Text="" KeyUp="TxtBoxAddress_KeyUp"></TextBox>
Option one would be to use a background image as described in the MSDN. If you want to use bindings this will probably be the best way.
XAML
<StackPanel>
<TextBox Name="myTextBox" TextChanged="OnTextBoxTextChanged" Width="200">
<TextBox.Background>
<ImageBrush ImageSource="TextBoxBackground.gif" AlignmentX="Left" Stretch="None" />
</TextBox.Background>
</TextBox>
</StackPanel>
</Page>
Code
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace SDKSample
{
public partial class TextBoxBackgroundExample : Page
{
void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
// Create an ImageBrush.
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
new Uri(@"TextBoxBackground.gif", UriKind.Relative)
);
textImageBrush.AlignmentX = AlignmentX.Left;
textImageBrush.Stretch = Stretch.None;
// Use the brush to paint the button's background.
myTextBox.Background = textImageBrush;
}
else
{
myTextBox.Background = null;
}
}
}
Or you could use a BooleanToVisibilityConverter as described in this codeproject article .