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; namespace PDF_Office.CustomControl { /// <summary> /// 带预文本和圆角的textbox /// </summary> public class TextBoxEx : TextBox { static TextBoxEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx))); } ~TextBoxEx() { var btn = base.GetTemplateChild("PART_BtnClear") as Button; if (btn != null) { //一定要取消事件挂载,否则可能无法被GC回收 btn.Click -= Btn_Click; } } public override void OnApplyTemplate() { //需要配合特定的Style才可用 var btn = base.GetTemplateChild("PART_BtnClear") as Button; if(btn!=null) { btn.Click += Btn_Click; } base.OnApplyTemplate(); } private void Btn_Click(object sender, RoutedEventArgs e) { this.Text = ""; this.Focus(); } public string PlaceholderText { get { return (string)GetValue(PlaceholderTextProperty); } set { SetValue(PlaceholderTextProperty, value); } } public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register("PlaceholderText", typeof(string), typeof(TextBoxEx), null); public CornerRadius CornerRadius { get { return (CornerRadius)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TextBoxEx), new PropertyMetadata(new CornerRadius(4))); public Brush PlaceholderForeground { get { return (Brush)GetValue(PlaceholderForegroundProperty); } set { SetValue(PlaceholderForegroundProperty, value); } } public static readonly DependencyProperty PlaceholderForegroundProperty = DependencyProperty.Register("PlaceholderForeground", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#94989C")))); public Brush MouseOverForeground { get { return (Brush)GetValue(MouseOverForegroundProperty); } set { SetValue(MouseOverForegroundProperty, value); } } public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush(Colors.Transparent))); } }