c# 4.0 - wpf mvvm datepicker textbox auto format -
is possible auto format user input date in textbox of datepicker?
i have following code
<style targettype="{x:type datepicker}"> <setter property="foreground" value="{dynamicresource textbrush}"/> <setter property="istodayhighlighted" value="true"/> <setter property="selecteddateformat" value="short"/> <setter property="padding" value="2"/> <setter property="calendarstyle" value="{dynamicresource datepickercalendarstyle}" /> </style> <style targettype="{x:type datepickertextbox}"> <setter property="foreground" value="{dynamicresource textbrush}"/> <setter property="padding" value="2"/> <setter property="control.template"> <setter.value> <controltemplate> <textbox x:name="part_textbox" text="{binding path=selecteddate, stringformat='dd/mm/yyyy', relativesource={relativesource ancestortype={x:type datepicker}}}" /> </controltemplate> </setter.value> </setter> </style>
and in view have
<datepicker height="25" horizontalalignment="left" margin="518,34,0,0" name="datethru" verticalalignment="top" width="115" selecteddate="{binding datethruselected, mode=twoway}" isenabled="{binding datethruisenabled}" > </datepicker>
so 2 issues. first when enter textbox "030613" or "03062013" , tap through expecting formated "03/06/2013" in textbox of datpicker automatically.
second expecting selecteddate property set "03/06/2013". (if pick date embedded calendar selecteddate binding works fine.)
my current code posted here neither of two. idea how can achieve 2 goals?
thanks in advance!
about first issue :
regex comes mind. ease of use, try using abstract class, , derive it, :
public abstract class datetimeregexpattern { public abstract datetime datetime { get; } public abstract bool ismatch(string input); }
for every pattern want recognized, derive abstract class (could enhanced), , put them in list. whenever textbox's content changes, in order detect pattern applied, use linq.
list<datetimeregexpattern> patterns = new list<datetimeregexpattern>(); // fill list 1 instance of each of derived classes of datetimeregexpattern datetimeregexpattern matching = patterns.firstordefault(x => x.ismatch("030613"));
then, retrieve datetime, if pattern matches, , apply datepicker, if current date different.
about second issue : http://msdn.microsoft.com/fr-fr/library/system.windows.controls.datepicker.selecteddatechanged.aspx event should trick. when date changes, change textbox's content, if dates different.
Comments
Post a Comment