WPF Custom control textbox with watermark and error message display

Source: Internet
Author: User

Often do the information editing interface when you encounter the need for watermark function, there are error messages display, must fill in the display and other functions. No uniform style specification is cumbersome. Just recently adjust the interface, find some information on the Internet, wrote a TextBox control, with watermark function, whether required, and error message display.

My language organization is not good, directly on the code

Interface code

  

<usercontrol x:class= "Textboxedit.selfwatemarktextbox" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:mc=" Http://schemas.open              xmlformats.org/markup-compatibility/2006 "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 " mc:ignorable= "D" > <Grid> <stackpanel orientation= "Horizontal" height= "+" > &L T Label name= "Isinput" foreground= "Red" verticalcontentalignment= "center" horizontalcontentalignment= "center" Width= "   ">*</Label> <textbox verticalcontentalignment=" Center "width=" "height=" X:name= "TextBox1" /> <border name= "Iserrorshowborder" borderthickness= "1" height= "cornerradius=" and "visibility=" Hi Dden "> <label name=" IsError "foreground=" Red "background=" Lightpink "verticalcontentalignment=" Cente R "Horizontalcontentalignment= "Center" > Error Messages </Label> </Border> <textbox verticalcontentalignment= "center" text= " {Binding iserroshow} "height=" X:name= "Txterrorshow" visibility= "Hidden"/> </StackPanel> </gri D></usercontrol>

Background code

  

Private Const string defaulttext = "";        Private Const string isinputtext = "";        Private Const string iserrortext = "";            Public Selfwatemarktextbox () {InitializeComponent (); This.            Loaded + = new Routedeventhandler (watemarktextbox_loaded); This.            GotFocus + = new Routedeventhandler (watemarktextbox_gotfocus); This.            LostFocus + = new Routedeventhandler (watemarktextbox_lostfocus);            This.textBox1.TextChanged + = new Textchangedeventhandler (textbox1_textchanged);        This.txtErrorShow.TextChanged + = new Textchangedeventhandler (txterrorshow_textchanged); } void Txterrorshow_textchanged (object sender, Textchangedeventargs e) {if (!string. Isnullorwhitespace (This.txtErrorShow.Text) && txtErrorShow.Text.ToLower () = = "true") {I            serrorshowborder.visibility = visibility.visible; } else {Iserrorshowborder.visiBility = Visibility.hidden; }} void textBox1_TextChanged (object sender, Textchangedeventargs e) {if (!string. Isnullorwhitespace (this.textBox1.Text) | |            this.textBox1.IsFocused) {this.textBox1.Text = This.textBox1.Text;            } else {this.textBox1.Text = Watermark; }} void Watemarktextbox_lostfocus (object sender, RoutedEventArgs e) {if (string. IsNullOrEmpty (This.textBox1.Text)) {This.textBox1.Text = string.            Empty; }} void Watemarktextbox_gotfocus (object sender, RoutedEventArgs e) {if (this.textBox1.Te Xt. Equals (Watermark)) {This.textBox1.Text = string.            Empty; }} void Watemarktextbox_loaded (object sender, RoutedEventArgs e) {this.textBox1.Text = W            Atermark; This. Isinput.content = IsRequired; This.        Iserror.content = Iserrormessage; } public string Watermark {get {string result = (string) GetValue (wate                Rmarkproperty); if (string.                IsNullOrEmpty (Result)) {result = DefaultText;            } return result;        } set {SetValue (Watermarkproperty, value);} } public string IsRequired {get {string result = (string) GetValue (IsR                Equiredproperty); if (string.                IsNullOrEmpty (Result)) {result = Isinputtext;            } return result;        } set {SetValue (Isrequiredproperty, value);} } public string Iserrormessage {get {string result = (string) GetValue                (Iserrormessageproperty); if (string. IsNullOrEmpty (Result))               {result = Iserrortext;            } return result;        } set {SetValue (Iserrormessageproperty, value);}                public string Iserrorshow {set {txterrorshow.text = null;            Txterrorshow.text = value; }} readonly DependencyProperty watermarkproperty = Dependencyproperty.register ("Watermark        ", typeof (String), typeof (Selfwatemarktextbox), New UIPropertyMetadata (DefaultText)); public static readonly DependencyProperty Isrequiredproperty = Dependencyproperty.register ("IsRequired", Typeo        F (String), typeof (Selfwatemarktextbox), New UIPropertyMetadata (Isinputtext)); public static readonly DependencyProperty Iserrormessageproperty = Dependencyproperty.register ("Iserrormessage" , typeof (String), typeof (Selfwatemarktextbox), New UIPropertyMetadata (Iserrortext));

MainWindow Interface Call Code

<my:selfwatemarktextbox  x:name= "WateMarkTextbox1" verticalalignment= "Top"  isrequired= "*"  watermark= "Please enter name" iserrormessage= "Please enter the normal name"  height= "/>        <button content=" OK "height=" width= " "click=" ></Button> "Button_Click"

MainWindow Interface Background Code

Private void Button_Click (object  sender, RoutedEventArgs e)        {            "true" ;        }

Final effect

When entered

  

Error message Display

  

According to Yang Youshan greatly prepared, thanks to Yang Youshan greatly to provide

http://blog.csdn.net/yysyangyangyangshan/article/details/9413237

  

WPF Custom control textbox with watermark and error message display

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.