using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace UnwindSoftwareLtd.WPF.StandardControls
{
public class TextBoxWaterMark : TextBox
{
private string waterMarkText = "Please enter username";
///
/// Text to be displayed as a water mark. (Default is Please enter username)
///
public string WaterMarkText
{
get { return waterMarkText; }
set { waterMarkText = value; }
}
private Brush waterMarkBackground = new SolidColorBrush(Colors.Beige);
///
/// Water Mark background color (Default is Brushes.Beige)
///
public Brush WaterMarkBackground
{
get { return waterMarkBackground; }
set { waterMarkBackground = value; }
}
private Brush waterMarkForeground = new SolidColorBrush(Colors.Gray);
///
/// Water Mark text Foreground (Default is Brushes.Gray)
///
public Brush WaterMarkForeground
{
get { return waterMarkForeground; }
set { waterMarkForeground = value; }
}
private Brush standardInputBackground = Brushes.White;
///
/// Standard input background (Default is Brushes.White)
///
public Brush StandardInputBackground
{
get { return standardInputBackground; }
set { standardInputBackground = value; }
}
private Brush standardInputForeground = Brushes.Black;
///
/// Standard Input Foreground (Default is Brushes.Black)
///
public Brush StandardInputForeground
{
get { return standardInputForeground; }
set { standardInputForeground = value; }
}
///
/// Textbox with a watermark background.
///
public TextBoxWaterMark()
{
WaterMark();
}
///
/// Check if watermark is displayed and swap over to standard background.
///
///
protected override void OnGotFocus(RoutedEventArgs e)
{
if (this.Text == waterMarkText)
{
this.Text = "";
StandardBackground();
}
base.OnGotFocus(e);
}
///
/// Check if watermark should be displayed if so setup.
///
///
protected override void OnLostFocus(RoutedEventArgs e)
{
this.Text = this.Text.Trim();
if (this.Text.Length == 0)
{
WaterMark();
}
base.OnLostFocus(e);
}
///
/// Sets standard background on the control.
///
private void StandardBackground()
{
this.Background = standardInputBackground;
this.Foreground = standardInputForeground;
}
///
/// Sets up a watermark on the textbox.
///
private void WaterMark()
{
this.Background = waterMarkBackground;
this.Foreground = waterMarkForeground;
this.Text = waterMarkText;
}
}
}