Saturday 15 January 2011

c# - XAML - refine watermark code -



c# - XAML - refine watermark code -

i creating log in page wpf application , want add together watermarks onto username , password fields. can accomplish duplicating code not satisfactory solution me. there way can pass string value through watermark binding seen in grid.resources section?

the goal refactor these 2 controls both can utilize single resource login hint below, have done research on came short on information. thanks

<grid.resources> <visualbrush x:key="loginhint" stretch="uniform" alignmentx="left" alignmenty="center" > <visualbrush.visual> <grid horizontalalignment="left"> <textblock fontfamily="corbel" horizontalalignment="left" verticalalignment="center" foreground="gray" fontstyle="italic" opacity="0.3" text="enter username"/> </grid> </visualbrush.visual> </visualbrush> </grid.resources> <textbox fontsize="24" borderbrush="#008ab8" borderthickness="1" grid.row="1" x:name="usernametextbox" horizontalalignment="stretch" verticalalignment="stretch" margin="25"> <textbox.style> <style targettype="{x:type textbox}"> <setter property="background" value="transparent"/> <style.triggers> <datatrigger binding="{binding elementname=usernametextbox,path=text}" value="" > <setter property="background" value="{staticresource loginhint}"/> </datatrigger> </style.triggers> </style> </textbox.style> </textbox> <passwordbox fontsize="24" borderbrush="#008ab8" borderthickness="1" grid.row="2" x:name="passwordmarktextbox" horizontalalignment="stretch" verticalalignment="stretch" margin="25"> <passwordbox.style> <style targettype="{x:type passwordbox}"> <setter property="background" value="transparent"/> <style.triggers> <datatrigger binding="{binding elementname=passwordmarktextbox,path=text}" value="" > <setter property="background" value="{staticresource loginhint}"/> </datatrigger> </style.triggers> </style> </passwordbox.style> </passwordbox>

i create custom attached property (e.g., watermarktext) , create new command templates bind value , superimpose watermarks on top of regular text area. utilize triggers rule visibility, showing when no text or password has been entered. may need separate, readonly (computed) property command visibility in order work both textbox , passwordbox.

c# wpf xaml binding

No comments:

Post a Comment