.net - How to reference a control template from a standard control using bindings? -
i building custom control inherits standard system.windows.controls.calendar control. i'm following article on code project. want make calendaritem sub object resizable, in article on msdn.
i copied style calendar, calendardaybutton , calendaritem source code of calendar control custom control resourcedictionary located in generic.xaml. calendaritem style references 3 button templates (the previous month, next month , header buttons) this:
<!-- start: previous button content --> <button x:name="part_previousbutton" grid.row="0" grid.column="0" template="{staticresource previousbuttontemplate}" height="20" width="28" horizontalalignment="left" focusable="false" /> <!-- end: previous button content --> <!-- start: header button content --> <button x:name="part_headerbutton" grid.row="0" grid.column="1" template="{staticresource headerbuttontemplate}" horizontalalignment="center" verticalalignment="center" fontweight="bold" fontsize="10.5" focusable="false" /> <!-- end: header button content --> <!-- start: next button content --> <button x:name="part_nextbutton" grid.row="0" grid.column="2" height="20" width="28" horizontalalignment="right" template="{staticresource nextbuttontemplate}" focusable="false" /> <!-- end: next button content -->
now because binding uses staticresource, need copy styles 3 buttons resourcedictionary. not planning changes these buttons, hoping having style them in resourcedictionary can avoided.
is there way can reference original styles these buttons via binding?
yes, there is:
on custom control inherits calendar
, can expose dependency properties of type controltemplate
each of 3 styles
public class mycalender : calendar { public static readonly dependencyproperty previousbuttontemplateproperty = dependencyproperty.register("previousbuttontemplate", typeof (controltemplate), typeof (mycalender), new propertymetadata(default(controltemplate))); public controltemplate previousbuttontemplate { { return (controltemplate) getvalue(previousbuttontemplateproperty); } set { setvalue(previousbuttontemplateproperty, value); } } // same other button templates }
and bind these styles within controltemplate
calendar using templatedparent
binding.
<controltemplate x:key="mycalender" targettype="viewmodels:mycalendar"> <grid> <button x:name="part_previousbutton" grid.row="0" grid.column="0" template="{templatebinding property=previousbuttontemplate}" height="20" width="28" horizontalalignment="left" focusable="false" /> </grid> </controltemplate>
i define default style on highest possible level, assigns button templates (previousbuttontemplate
, etc.) dependency properties of custom calender control. highest possible level, mean object, in scope templates exist, wire together, example app.xaml or window.
<style targettype="viewmodels:mycalendar"> <setter property="previousbuttontemplate" value="{staticresource previousbuttontemplate}" /> </style>
alternatively, can leave is, , long staticresources
, control template refers exist during runtime, resolved correctly. not nice, though , no design time support.
edit
ok, didn't referring 'original' button templates, thought meant templates defined somewhere else. then: no, can't this. i've been trying same thing number of times, can't create control templates use parts of original ones. there no such thing 'basedon' controltemplates.
Comments
Post a Comment