windows phone 水印TextBox

原理:在失去焦点和获取焦点的时候,判断Text值是否为空或者是否与水印值相同,然后修改TextBox中的Text和Foreground。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ==============================================================================
 2   * 类名称:WatermarkTextBox
 3   * 类描述:
 4   * 创建人:neoyee
 5   * 创建时间:2014/2/25 17:24:11
 6   * 修改人:
 7   * 修改时间:
 8   * 修改备注:
 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;
            }
        }
    }
}

windows phone 水印TextBox

上一篇:USACO 3.1 Shaping Regions (rect1)


下一篇:[转]Win7下Eclipse中文字体太小