c# - How to set watermark text on watermark textbox -
i have template this,
<style x:key="watermarktextboxstyle" basedon="{staticresource {x:type textbox}}" targettype="{x:type textbox}"> <setter property="template"> <setter.value> <controltemplate targettype="{x:type textbox}"> <grid> <scrollviewer x:name="part_contenthost" snapstodevicepixels="{templatebinding snapstodevicepixels}" /> <textblock x:name="textblock" opacity="0.345" text="enter text here" textwrapping="wrap" visibility="hidden" /> </grid> <controltemplate.triggers> <multitrigger> <multitrigger.conditions> <condition property="isfocused" value="false" /> <condition property="text" value="" /> </multitrigger.conditions> <setter property="visibility" targetname="textblock" value="visible" /> </multitrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
this seems work quite nicely watermark textbox in wpf, how can change watermark text be?
above hard coded text = 'enter text here'.
if use above this,
<textbox style="{staticresource watermarktextboxstyle}"></textbox>
i cannot set watermark text is.
ideas?
use attached dependency property:
public static class watermark { public static readonly dependencyproperty textproperty = dependencyproperty.registerattached( "text", typeof(boolean), typeof(watermark), new frameworkpropertymetadata() ); public static void settext( uielement element, boolean value ) { element.setvalue( textproperty, value ); } public static boolean gettext( uielement element ) { return (boolean)element.getvalue( textproperty ); } }
then control you'd like:
<textbox style="{staticresource watermarktextboxstyle}" watermark.text="search" />
your style need bind dp:
<textblock x:name="textblock" opacity="0.345" text="{binding relativesource={relativesource templatedparent}, path=(local:watermark.text)}" textwrapping="wrap" visibility="hidden" />
Comments
Post a Comment