Windows Phone watermark textbox

Source: Internet
Author: User

The original from: WP Tutorial network

Principle: When you lose focus and get focus, determine if the text value is empty or is the same as the watermark value, and then modify the text and foreground in the TextBox.

The code is as follows:

/* ==============================================================================
2 * Class Name: Watermarktextbox
3 * Class Description:
4 * Creator: Neoyee
5 * Creation time: 2014/2/25 17:24:11
6 * Modification of person:
7 * Modification Time:
8 * Modify NOTES:
9 * @version 1.0
10 * ==============================================================================*/

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows;
Using System.Windows.Controls;
Using System.Windows.Media;
Using Windows.ui;


Namespace WP8. Controls
{
public sealed class Watermarktextbox:textbox
{
private static readonly DependencyProperty Watermarktextproperty =
Dependencyproperty.register ("Watermarktext", typeof (String), typeof (Watermarktextbox), new PropertyMetadata (string . Empty, New Propertychangedcallback (watermarktextchanged));


private static readonly DependencyProperty Watermarkforegroundproperty =
Dependencyproperty.register ("Watermarkforeground", typeof (SolidColorBrush), typeof (Watermarktextbox), new PropertyMetadata (New SolidColorBrush (Colors.black)));

private static readonly DependencyProperty Watermarkbackgroundproperty =
Dependencyproperty.register ("Watermarkbackground", typeof (SolidColorBrush), typeof (Watermarktextbox), new PropertyMetadata (New SolidColorBrush (Colors.white)));

private static readonly DependencyProperty Normalforegroundproperty =
Dependencyproperty.register ("Normalforeground", typeof (SolidColorBrush), typeof (Watermarktextbox), new PropertyMetadata (New SolidColorBrush (Colors.black), normalforegroundpropertychanged));

private static readonly DependencyProperty Normalbackgroundproperty =
Dependencyproperty.register ("Normalbackground", typeof (SolidColorBrush), typeof (Watermarktextbox), new PropertyMetadata (New SolidColorBrush (Colors.white)));

private static void Normalforegroundpropertychanged (DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var watermarktextbox = obj as Watermarktextbox;
if (Watermarktextbox! = null)
Watermarktextbox.normalforegroundchanged ((SolidColorBrush) args. NewValue);
}

private void Normalforegroundchanged (SolidColorBrush value)
{
Foreground = value;
}

Public SolidColorBrush Normalbackground
{
get {return (SolidColorBrush) GetValue (normalbackgroundproperty);}
set {SetValue (Normalbackgroundproperty, value);}
}

Public SolidColorBrush Normalforeground
{
get {return (SolidColorBrush) GetValue (normalforegroundproperty);}
set {SetValue (Normalforegroundproperty, value);}
}
Public SolidColorBrush Watermarkbackground
{
get {return (SolidColorBrush) GetValue (watermarkbackgroundproperty);}
set {SetValue (Watermarkbackgroundproperty, value);}
}
Public SolidColorBrush Watermarkforeground
{
get {return (SolidColorBrush) GetValue (watermarkforegroundproperty);}
set {SetValue (Watermarkforegroundproperty, value);}
}

public string Watermarktext
{
get {return (string) GetValue (Watermarktextproperty);}
set {SetValue (Watermarktextproperty, value);}
}

Public Watermarktextbox ()
{
This. LostFocus + = Watermarktextbox_lostfocus;
This. GotFocus + = Watermarktextbox_gotfocus;
This. TextChanged + = watermarktextbox_textchanged;
if (string. IsNullOrEmpty (this. Text))
{
This. Text = Watermarktext;
Foreground = Watermarkforeground;
}

}

void Watermarktextbox_textchanged (object sender, Textchangedeventargs e)
{

if (Text = = Watermarktext)
{
This. Text = Watermarktext;
Foreground = Watermarkforeground;
}
else if (Text! = watermarktext)
{
Foreground = Normalforeground;
}
}

private static void Watermarktextchanged (DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
((Watermarktextbox) obj). Watermarktextchanged (args. OldValue, args. NewValue);
}

private void Watermarktextchanged (object OldValue, Object NewValue)
{

}

void Watermarktextbox_gotfocus (object sender, RoutedEventArgs e)
{
if (this. Text = = Watermarktext && Foreground = = watermarkforeground)
{
This. Text = string. Empty;
Foreground = Normalforeground;
}
}

void Watermarktextbox_lostfocus (object sender, RoutedEventArgs e)
{
if (string. IsNullOrEmpty (this. Text) | | Text = = Watermarktext)
{
This. Text = Watermarktext;
Foreground = Watermarkforeground;
}
}
}
}

Detailed Description: http://wp.662p.com/thread-8105-1-1.html

Related Article

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.