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 { /// /// 带预文本和圆角的textbox /// public class TextBoxEx : TextBox { static TextBoxEx() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx))); } public override void OnApplyTemplate() { //需要配合特定的Style才可用 var btn = base.GetTemplateChild("PART_BtnClear") as Button; if(btn!=null) { WeakEventManager.AddHandler(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))); public bool IsError { get { return (bool)GetValue(IsErrorProperty); } set { SetValue(IsErrorProperty, value); } } // Using a DependencyProperty as the backing store for IsError. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false)); public Visibility ShowTipText { get { return (Visibility)GetValue(ShowTipTextProperty); } set { SetValue(ShowTipTextProperty, value); } } // Using a DependencyProperty as the backing store for ShowTipText. This enables animation, styling, binding, etc... public static readonly DependencyProperty ShowTipTextProperty = DependencyProperty.Register("ShowTipText", typeof(Visibility), typeof(TextBoxEx), new PropertyMetadata(Visibility.Collapsed)); public bool ShowTip { get { return (bool)GetValue(ShowTipProperty); } set { SetValue(ShowTipProperty, value); } } // Using a DependencyProperty as the backing store for ShowTip. This enables animation, styling, binding, etc... public static readonly DependencyProperty ShowTipProperty = DependencyProperty.Register("ShowTip", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false)); public string TipText { get { return (string)GetValue(TipTextProperty); } set { SetValue(TipTextProperty, value); } } // Using a DependencyProperty as the backing store for TipText. This enables animation, styling, binding, etc... public static readonly DependencyProperty TipTextProperty = DependencyProperty.Register("TipText", typeof(string), typeof(TextBoxEx), new PropertyMetadata("")); public bool ShowClose { get { return (bool)GetValue(ShowCloseProperty); } set { SetValue(ShowCloseProperty, value); } } // Using a DependencyProperty as the backing store for ShowClose. This enables animation, styling, binding, etc... public static readonly DependencyProperty ShowCloseProperty = DependencyProperty.Register("ShowClose", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(true)); } }