1: using System.Windows;
2: using System.Windows.Controls;
3: using System.Windows.Media;
4:
5: namespace SayHello
6: { 7: public partial class Page : UserControl
8: { 9: public Page()
10: { 11: InitializeComponent();
12: }
13:
14: private void txtName_GotFocus(object sender, RoutedEventArgs e)
15: { 16: if (string.IsNullOrEmpty(txtName.Text) || txtName.Text == "請輸入姓名")
17: { 18: txtName.Text = string.Empty;
19: }
20: }
21:
22: private void txtName_TextChanged(object sender, TextChangedEventArgs e)
23: { 24: if (string.IsNullOrEmpty(txtName.Text) || txtName.Text == "請輸入姓名")
25: { 26: txtName.Foreground = new SolidColorBrush(Color.FromArgb(255, 204, 204, 204));
27: txtName.Background = new SolidColorBrush(Color.FromArgb(255, 240, 248, 255));
28: }
29: else
30: { 31: txtName.Foreground = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
32: txtName.Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
33: }
34: }
35:
36: private void btnSubmit_Click(object sender, RoutedEventArgs e)
37: { 38: if (string.IsNullOrEmpty(txtName.Text) || txtName.Text == "請輸入姓名")
39: { 40: txtName.Text = "請輸入姓名";
41: txtResult.Text = string.Empty;
42: return;
43: }
44:
45: txtResult.Text = string.Format("哈囉,{0}!", txtName.Text); 46: }
47:
48: private void btnReset_Click(object sender, RoutedEventArgs e)
49: { 50: txtName.Text = "請輸入姓名";
51: txtResult.Text = string.Empty;
52: }
53: }
54: }