[WPF 学习] 14.PlaceHolder的简单实现
程序员文章站
2022-03-25 17:19:32
为了能实现PlaceHolder的效果,用 "https://www.cnblogs.com/catzhou/p/12658478.html" 的方式,实在是十分麻烦,要为每个文本框都折腾一把,实在不爽。现在介绍个简单的方法,借用Tag属性传递PlaceHolder的内容,代码如下: 哦,顺便弄了个 ......
为了能实现placeholder的效果,用[]([wpf 学习] 12.动态绑定visualbrush)的方式,实在是十分麻烦,要为每个文本框都折腾一把,实在不爽。现在介绍个简单的方法,借用tag属性传递placeholder的内容,代码如下:
<window.resources> <system:string x:key="placeholderen">english</system:string> <system:string x:key="placeholdercn">中文</system:string> <controltemplate x:key="textboxtemplateplaceholder" targettype="textbox"> <border x:name="border" background="{templatebinding background}" borderthickness="{templatebinding borderthickness}" borderbrush="{templatebinding borderbrush}" snapstodevicepixels="true" cornerradius="15" padding="5,0"> <grid> <scrollviewer x:name="part_contenthost" focusable="false" horizontalscrollbarvisibility="hidden" verticalscrollbarvisibility="hidden"/> <textblock x:name="placeholder" text="{templatebinding tag}" horizontalalignment="center" verticalalignment="center" opacity="0" background="transparent"></textblock> </grid> </border> <controltemplate.triggers> <trigger property="text" value=""> <setter targetname="placeholder" property="opacity" value="0.3"></setter> </trigger> </controltemplate.triggers> </controltemplate> </window.resources> <stackpanel name="abc"> <textbox fontsize="40" text="" tag="{dynamicresource placeholderen}" name="aaa" template="{staticresource textboxtemplateplaceholder}"/> <textbox fontsize="40" text="" tag="{dynamicresource placeholdercn}" template="{staticresource textboxtemplateplaceholder}"/> </stackpanel> </window>
哦,顺便弄了个圆角,那叫一个方便,爽!