水印文本框
程序员文章站
2024-02-07 20:43:40
先看下效果: 流程: 1.新建WatermarkTextBox类,继承自TextBox。添加依赖项属性Watermark(水印)、WatermarkForeground(水印前景色)。 1 public class WatermarkTextBox : TextBox 2 { 3 #region D ......
先看下效果:
流程:
1.新建watermarktextbox类,继承自textbox。添加依赖项属性watermark(水印)、watermarkforeground(水印前景色)。
1 public class watermarktextbox : textbox 2 { 3 #region dependencyproperties 4 public string watermark 5 { 6 get { return (string)getvalue(watermarkproperty); } 7 set { setvalue(watermarkproperty, value); } 8 } 9 10 // using a dependencyproperty as the backing store for watermark. this enables animation, styling, binding, etc... 11 public static readonly dependencyproperty watermarkproperty = 12 dependencyproperty.register("watermark", typeof(string), typeof(watermarktextbox), new propertymetadata(default(string))); 13 14 public brush watermarkforeground 15 { 16 get { return (brush)getvalue(watermarkforegroundproperty); } 17 set { setvalue(watermarkforegroundproperty, value); } 18 } 19 20 // using a dependencyproperty as the backing store for watermarkforeground. this enables animation, styling, binding, etc... 21 public static readonly dependencyproperty watermarkforegroundproperty = 22 dependencyproperty.register("watermarkforeground", typeof(brush), typeof(watermarktextbox), new propertymetadata(new solidcolorbrush(colors.gray))); 23 24 #endregion 25 26 static watermarktextbox() 27 { 28 defaultstylekeyproperty.overridemetadata(typeof(watermarktextbox), new frameworkpropertymetadata(typeof(watermarktextbox))); 29 } 30 }
2.编写watermarktextbox样式。
1 <style x:key="watermarktextboxstyle" targettype="{x:type local:watermarktextbox}"> 2 <setter property="snapstodevicepixels" value="true" /> 3 <setter property="horizontalcontentalignment" value="left" /> 4 <setter property="verticalcontentalignment" value="center" /> 5 <setter property="borderthickness" value="1" /> 6 <setter property="borderbrush" value="black" /> 7 <setter property="padding" value="4,2" /> 8 <setter property="template"> 9 <setter.value> 10 <controltemplate targettype="{x:type local:watermarktextbox}"> 11 <border background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" snapstodevicepixels="{templatebinding snapstodevicepixels}"> 12 <grid> 13 <scrollviewer name="part_contenthost" focusable="false" horizontalscrollbarvisibility="hidden" verticalscrollbarvisibility="hidden" /> 14 <textblock x:name="part_watermark" 15 margin="{templatebinding padding}" 16 horizontalalignment="{templatebinding horizontalcontentalignment}" 17 verticalalignment="{templatebinding verticalcontentalignment}" 18 fontfamily="{templatebinding fontfamily}" 19 fontsize="{templatebinding fontsize}" 20 fontweight="{templatebinding fontweight}" 21 foreground="{templatebinding watermarkforeground}" 22 text="{templatebinding watermark}" 23 visibility="collapsed" /> 24 </grid> 25 </border> 26 <controltemplate.triggers> 27 <multitrigger> 28 <multitrigger.conditions> 29 <condition property="isfocused" value="false" /> 30 <condition property="text" value="" /> 31 </multitrigger.conditions> 32 <setter targetname="part_watermark" property="visibility" value="visible" /> 33 </multitrigger> 34 <trigger property="ismouseover" value="true"> 35 <setter property="borderbrush" value="#ff7eb4ea" /> 36 </trigger> 37 <trigger property="iskeyboardfocused" value="true"> 38 <setter property="borderbrush" value="#ff569de5" /> 39 </trigger> 40 <trigger property="isenabled" value="false"> 41 <setter property="opacity" value="0.56" /> 42 </trigger> 43 </controltemplate.triggers> 44 </controltemplate> 45 </setter.value> 46 </setter> 47 </style>
3.界面引用。
1 <stackpanel horizontalalignment="center" verticalalignment="center" orientation="horizontal"> 2 <local:watermarktextbox x:name="txt" 3 width="200" height="28" 4 style="{staticresource watermarktextboxstyle}" 5 watermark="请输入..." /> 6 <button width="60" height="28" 7 margin="10,0,0,0" 8 click="btnconfirm_click" content="确定" /> 9 </stackpanel>