基本信息
源码名称:C# wpf textbox 样式实例(水印)源码
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2014-06-06
   源码介绍

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace TestTextBoxWaterMark
{
    public class WateMarkTextBox:TextBox
    {
        private TextBlock wateMarkTextBlock;

        private TextBox wateMarkTextBox;

          static WateMarkTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WateMarkTextBox), new FrameworkPropertyMetadata(typeof(WateMarkTextBox)));
        }

          public WateMarkTextBox()
        {
            this.GotFocus  = new RoutedEventHandler(WateMarkTextBox_GotFocus);

            this.LostFocus  = new RoutedEventHandler(WateMarkTextBox_LostFocus);

            this.TextChanged  = new TextChangedEventHandler(WateMarkTextBox_TextChanged);
        }

          void WateMarkTextBox_LostFocus(object sender, RoutedEventArgs e)
          {
              if (this.wateMarkTextBox!=null && !string.IsNullOrWhiteSpace(this.wateMarkTextBox.Text))
              {
                  this.wateMarkTextBlock.Visibility = Visibility.Hidden;
              }
              else
              {
                  this.wateMarkTextBlock.Visibility = Visibility.Visible;
              }
          }

          void WateMarkTextBox_GotFocus(object sender, RoutedEventArgs e)
          {
              this.wateMarkTextBlock.Visibility = Visibility.Hidden;
          }

          void WateMarkTextBox_TextChanged(object sender, TextChangedEventArgs e)
          {
              if (this.wateMarkTextBox != null && !string.IsNullOrWhiteSpace(this.wateMarkTextBox.Text) || !this.IsFocused)
              {
                  this.wateMarkTextBlock.Visibility = Visibility.Hidden;
              }
              else
              {
                  this.wateMarkTextBlock.Visibility = Visibility.Visible;
              }
          }  


        public string WateMark
        {
            get { return (string)GetValue(WateMarkProperty); }

            set { SetValue(WateMarkProperty, value); }
        }

        public static DependencyProperty WateMarkProperty =
            DependencyProperty.Register("WateMark", typeof(string), typeof(WateMarkTextBox), new UIPropertyMetadata("水印"));

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.wateMarkTextBlock = this.GetTemplateChild("ChildWateMark") as TextBlock;

            this.wateMarkTextBox = this.GetTemplateChild("ChildTextBox") as TextBox;
        }
    }
}