How to add events to templated control in silverlight? -
bounty rewarded solid tutorial/learning resources regarding wiring events templated controls.
i have control template this:
<style targettype="local:datepicker"> <setter property="template"> <setter.value> <controltemplate targettype="local:datepicker"> <border background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" x:name="mydatepickercontentarea"> <stackpanel orientation="vertical"> <button x:name="mytestbutton" content="test button" /> <telerik:raddatepicker style="{staticresource visitsreporttextboxstyle}" foreground="#ffffff" x:name="startdate" datetimewatermarkcontent="start date"/> <telerik:raddatepicker style="{staticresource visitsreporttextboxstyle}" x:name="enddate" datetimewatermarkcontent="end date"/> </stackpanel> </border> </controltemplate> </setter.value> </setter> </style>
the c# template is:
public class datepicker : control { public static readonly dependencyproperty startdateselecteddateproperty = dependencyproperty.register("startdateselecteddateproperty", typeof(datetime), typeof(datepicker), null); public datetime? startdateselecteddate { get; set; } public datepicker() { this.defaultstylekey = typeof(datepicker); } public override void onapplytemplate() { raddatepicker startdate = this.gettemplatechild("startdate") raddatepicker; startdate.selectionchanged += new telerik.windows.controls.selectionchangedeventhandler(startdate_selectionchanged); startdate.selecteddate = new datetime(2010, 01, 01); base.onapplytemplate(); } void startdate_selectionchanged(object sender, telerik.windows.controls.selectionchangedeventargs e) { raddatepicker temp = (raddatepicker)sender; startdateselecteddate = temp.selecteddate; } }
my selectionchanged event doesn't fire , i'm not sure why. ideas ?
here example of using best practice sort of control think attempting build (see notes @ end explanations):-
[templatepart(name = datepicker.elementstartdate, type = typeof(raddatepicker))] [templatepart(name = datepicker.elementenddate, type = typeof(raddatepicker))] public class datepicker : control { public datepicker() { this.defaultstylekey = typeof(datepicker); } #region template part names private const string elementstartdate = "startdate"; private const string elementenddate = "enddate"; #endregion #region template parts private raddatepicker _startdate; internal raddatepicker startdate { { return _startdate; } private set { if (_startdate != null) { _startdate.selectionchanged -= startdate_selectionchanged; } _startdate = value; if (_startdate != null) { _startdate.selectionchanged += startdate_selectionchanged; } } } private raddatepicker _enddate; internal raddatepicker enddate { { return _enddate; } private set { if (_enddate!= null) { _enddate.selectionchanged -= enddate_selectionchanged; } _enddate= value; if (_enddate!= null) { _enddate.selectionchanged += enddate_selectionchanged; } } } #endregion public static readonly dependencyproperty startdateselecteddateproperty = dependencyproperty.register( "startdateselecteddateproperty", typeof(datetime?), typeof(datepicker), new propertymetadata(new datetime(2010, 01, 01))); public datetime? startdateselecteddate { { return (datetime?)getvalue(startdateselecteddateproperty); } set { setvalue(startdateselecteddateproperty)} } public static readonly dependencyproperty enddateselecteddateproperty = dependencyproperty.register( "enddateselecteddateproperty", typeof(datetime?), typeof(datepicker), new propertymetadata(new datetime(2010, 01, 01))); public datetime? enddateselecteddate { { return (datetime?)getvalue(enddateselecteddateproperty); } set { setvalue(enddateselecteddateproperty)} } public override void onapplytemplate() { base.onapplytemplate(); startdate = gettemplatechild(elementstartdate) raddatepicker; enddate = gettemplatechild(elementenddate) raddatepicker; } void startdate_selectionchanged(object sender, telerik.windows.controls.selectionchangedeventargs e) { // stuff startdate here } void enddate_selectionchanged(object sender, telerik.windows.controls.selectionchangedeventargs e) { // stuff enddate here } }
the template xaml should like:-
<style targettype="local:datepicker"> <setter property="template"> <setter.value> <controltemplate targettype="local:datepicker"> <border background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" x:name="mydatepickercontentarea"> <stackpanel orientation="vertical"> <button x:name="mytestbutton" content="test button" /> <telerik:raddatepicker x:name="startdate" style="{staticresource visitsreporttextboxstyle}" foreground="#ffffff" datetimewatermarkcontent="start date" selecteddate="{templatebinding startdateselecteddate}" /> <telerik:raddatepicker x:name="enddate" style="{staticresource visitsreporttextboxstyle}" datetimewatermarkcontent="end date" selecteddate="{templatebinding enddateselecteddate}" /> </stackpanel> </border> </controltemplate> </setter.value> </setter> </style>
some explanations
- a key problem original code had hadn't implemented dependency properties correctly. note properties use
getvalue
,setvalue
, property meta data used assign default rather attempting set in in onapplytemplate. - with properties correctly implemented template binding should work , in fact we're done far getting appears original intent, hence i've left out actual code in event handlers.
- create constants in code hold names of key template parts want interact with, allows name changes less expensive make.
- add
templatepart
attributes class indicate key elements code expects find, names should , base type expected have. allows designer re-template existing control, long declared template parts present somewher control should function correctly if ui radically altered. - if need attach event handlers elements create field hold reference element , create property wrap round it. property setter should detach , attach event handlers see in code.
- make sure bae.onapplytemplate called in override of onapplytemplate can see quite straight forward assign above created properties.
- i don't have raddatepicker can't test, outstanding concern
datetime?
correct type selecteddate property. if improvement on microsoft offering seems have drop ball on typical data entry requirement.
Comments
Post a Comment