扩展控件,顾名思义就是对已有的控件进行扩展,一般继承于已有的原生控件,不排除继承于自定义的控件,不过这样做意义不大,因为既然都自定义了,为什么不一步到位呢,有些不同的需求也可以通过此来完成,不过类似于类继承了。扩展控件本质也是类的继承。下面我们通过两个例子说明
一、自定义MButton
- 控件外观控制的属性,如圆角、鼠标悬浮前景色背景色、是否开启动画(鼠标悬停时小图标转一圈,移开又转回去)、鼠标按下颜色等;
- 字体图标相关属性,如字符值、字体图标大小、字体图标间距等。
- 首先重写要修改的属性和添加要扩展的功能(在.cs中)
-
public partial class MButton : Button { public static readonly DependencyProperty PressedBackgroundProperty = DependencyProperty.Register("PressedBackground", typeof(Brush), typeof(MButton), new PropertyMetadata(Brushes.DarkBlue)); ///
/// 鼠标按下背景样式 /// public Brush PressedBackground { get { return (Brush)GetValue(PressedBackgroundProperty); } set { SetValue(PressedBackgroundProperty, value); } } public static readonly DependencyProperty PressedForegroundProperty = DependencyProperty.Register("PressedForeground", typeof(Brush), typeof(MButton), new PropertyMetadata(Brushes.White)); ////// 鼠标按下前景样式(图标、文字) /// public Brush PressedForeground { get { return (Brush)GetValue(PressedForegroundProperty); } set { SetValue(PressedForegroundProperty, value); } } public static readonly DependencyProperty MouseOverBackgroundProperty = DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(MButton), new PropertyMetadata(Brushes.RoyalBlue)); ////// 鼠标进入背景样式 /// public Brush MouseOverBackground { get { return (Brush)GetValue(MouseOverBackgroundProperty); } set { SetValue(MouseOverBackgroundProperty, value); } } public static readonly DependencyProperty MouseOverForegroundProperty = DependencyProperty.Register("MouseOverForeground", typeof(Brush), typeof(MButton), new PropertyMetadata(Brushes.White)); ////// 鼠标进入前景样式 /// public Brush MouseOverForeground { get { return (Brush)GetValue(MouseOverForegroundProperty); } set { SetValue(MouseOverForegroundProperty, value); } } public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(MButton), new PropertyMetadata(new CornerRadius(2))); ////// 按钮圆角大小,左上,右上,右下,左下 /// public CornerRadius CornerRadius { get { return (CornerRadius)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } } public static readonly DependencyProperty ContentDecorationsProperty = DependencyProperty.Register( "ContentDecorations", typeof(TextDecorationCollection), typeof(MButton), new PropertyMetadata(null)); public TextDecorationCollection ContentDecorations { get { return (TextDecorationCollection)GetValue(ContentDecorationsProperty); } set { SetValue(ContentDecorationsProperty, value); } } static FButton() { DefaultStyleKeyProperty.OverrideMetadata(typeof(FButton), new FrameworkPropertyMetadata(typeof(FButton))); } }
下面是模板:
Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path= FIcon}"
FontSize="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path= FIconSize}"
Foreground="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path= Foreground}">
下面是样式:
这样我们就万行了扩展Button
二、自定义TextBox
这个TextBox可设置水印,可设置必填和正则表达式验证。
就是在输入完成后,控件一旦失去焦点就会自动验证!会根据我开放出来的“是否可以为空”属性进行验证,一旦为空,则控件变为警告样式。
但这还不是最特别的,为了各种手机号啊,邮箱啊的验证,我还开放了一个正则表达式的属性,在这个属性中填上正则表达式,同上, 一旦失去焦点就会自动验证输入的内容能否匹配正则表达式,如果不能匹配,则控件变为警告样式。
之后,代码还可以通过我开放的另一个属性来判断当前输入框的输入是否有误。
14 49
再来看看CS:
1 using System; 2 using System.Windows; 3 using System.Windows.Controls; 4 using System.Windows.Media; 5 using System.Windows.Input; 6 using System.Text.RegularExpressions; 7 8 namespace KAN.WPF.XCtrl.Controls 9 { 10 ///11 /// 扩展输入框:可设置水印,可设置必填,可设置正则表达式验证 12 /// 13 public class XTextBox:TextBox 14 { 15 #region 依赖属性 16 public static readonly DependencyProperty XWmkTextProperty;//水印文字 17 public static readonly DependencyProperty XWmkForegroundProperty;//水印着色 18 public static readonly DependencyProperty XIsErrorProperty;//是否字段有误 19 public static readonly DependencyProperty XAllowNullProperty;//是否允许为空 20 public static readonly DependencyProperty XRegExpProperty;//正则表达式 21 #endregion 22 23 #region 内部方法 24 ///25 /// 注册事件 26 /// 27 public XTextBox() 28 { 29 this.LostFocus += new RoutedEventHandler(XTextBox_LostFocus); 30 this.GotFocus += new RoutedEventHandler(XTextBox_GotFocus); 31 this.PreviewMouseDown += new MouseButtonEventHandler(XTextBox_PreviewMouseDown); 32 } 33 34 ///35 /// 静态构造函数 36 /// 37 static XTextBox() 38 { 39 //注册依赖属性 40 XTextBox.XWmkTextProperty = DependencyProperty.Register("XWmkText", typeof(String), typeof(XTextBox), new PropertyMetadata(null)); 41 XTextBox.XAllowNullProperty = DependencyProperty.Register("XAllowNull", typeof(bool), typeof(XTextBox), new PropertyMetadata(true)); 42 XTextBox.XIsErrorProperty = DependencyProperty.Register("XIsError", typeof(bool), typeof(XTextBox), new PropertyMetadata(false)); 43 XTextBox.XRegExpProperty = DependencyProperty.Register("XRegExp", typeof(string), typeof(XTextBox), new PropertyMetadata("")); 44 XTextBox.XWmkForegroundProperty = DependencyProperty.Register("XWmkForeground", typeof(Brush), 45 typeof(XTextBox), new PropertyMetadata(Brushes.Silver)); 46 FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(XTextBox), new FrameworkPropertyMetadata(typeof(XTextBox))); 47 } 48 49 ///50 /// 失去焦点时检查输入 51 /// 52 /// 53 /// 54 void XTextBox_LostFocus(object sender, RoutedEventArgs e) 55 { 56 this.XIsError = false; 57 if (XAllowNull == false && this.Text.Trim() == "") 58 { 59 this.XIsError = true; 60 } 61 if (Regex.IsMatch(this.Text.Trim(), XRegExp) == false) 62 { 63 this.XIsError = true; 64 } 65 } 66 67 ///68 /// 获得焦点时选中文字 69 /// 70 /// 71 /// 72 void XTextBox_GotFocus(object sender, RoutedEventArgs e) 73 { 74 this.SelectAll(); 75 } 76 77 ///78 /// 鼠标点击时选中文字 79 /// 80 /// 81 /// 82 void XTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) 83 { 84 if (this.IsFocused == false) 85 { 86 TextBox textBox = e.Source as TextBox; 87 textBox.Focus(); 88 e.Handled = true; 89 } 90 } 91 #endregion 92 93 #region 公布属性 94 ///95 /// 公布属性XWmkText(水印文字) 96 /// 97 public String XWmkText 98 { 99 get 100 { 101 return base.GetValue(XTextBox.XWmkTextProperty) as String; 102 } 103 set 104 { 105 base.SetValue(XTextBox.XWmkTextProperty, value); 106 } 107 } 108 109 ///110 /// 公布属性XWmkForeground(水印着色) 111 /// 112 public Brush XWmkForeground 113 { 114 get 115 { 116 return base.GetValue(XTextBox.XWmkForegroundProperty) as Brush; 117 } 118 set 119 { 120 base.SetValue(XTextBox.XWmkForegroundProperty, value); 121 } 122 } 123 124 ///125 /// 公布属性XIsError(是否字段有误) 126 /// 127 public bool XIsError 128 { 129 get 130 { 131 return (bool)base.GetValue(XTextBox.XIsErrorProperty); 132 } 133 set 134 { 135 base.SetValue(XTextBox.XIsErrorProperty, value); 136 } 137 } 138 139 ///140 /// 公布属性XAllowNull(是否允许为空) 141 /// 142 public bool XAllowNull 143 { 144 get 145 { 146 return (bool)base.GetValue(XTextBox.XAllowNullProperty); 147 } 148 set 149 { 150 base.SetValue(XTextBox.XAllowNullProperty, value); 151 } 152 } 153 154 ///155 /// 公布属性XRegExp(正则表达式) 156 /// 157 public string XRegExp 158 { 159 get 160 { 161 return base.GetValue(XTextBox.XRegExpProperty) as string; 162 } 163 set 164 { 165 base.SetValue(XTextBox.XRegExpProperty, value); 166 } 167 } 168 #endregion 169 } 170 }
好了,扩展控件就介绍完了
自定义控件系列博文链接:
WPF自定义控件(一)の控件分类
WPF自定义控件(二)の重写原生控件样式模板
WPF自定义控件(三)の扩展控件
WPF自定义控件(四)の自定义控件
WPF自定义控件(五)の用户控件