wpf - How can I change the DataTemplate for DayTitleTemplate in a CalendarItemTemplate -
when try change default datatemplate daytitletemplate in calendar, if root element not textblock, application crashes.
does knows how avoid crash?
here simplified version of template demonstration purposes. (kaxaml ready)
<page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <grid> <calendar> <calendar.calendaritemstyle> <style targettype="calendaritem"> <setter property="template"> <setter.value> <controltemplate targettype="calendaritem"> <controltemplate.resources> <!-- ___________________________________________________________________ --> <!-- default data template --> <datatemplate x:key="{componentresourcekey resourceid=daytitletemplate, typeintargetassembly={x:type calendaritem}}"> <textblock horizontalalignment="center" margin="0,6" verticalalignment="center" fontfamily="verdana" fontsize="9.5" fontweight="bold" foreground="#ff333333" text="{binding}"/> </datatemplate> <!-- ___________________________________________________________________ --> <!-- modified data template, using template instead of default, makes xaml crash. --> <!--<datatemplate x:key="{componentresourcekey resourceid=daytitletemplate, typeintargetassembly={x:type calendaritem}}"> <grid> <rectangle stroke="brown" strokethickness="0.5"/> <textblock foreground="#ff333333" fontweight="bold" fontsize="9.5" fontfamily="verdana" horizontalalignment="center" text="{binding}" verticalalignment="center"/> </grid> </datatemplate>--> <!-- ___________________________________________________________________ --> </controltemplate.resources> <grid x:name="part_root"> <border background="{templatebinding background}" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" cornerradius="1"> <border borderbrush="white" borderthickness="2" cornerradius="1"> <grid> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> </grid.rowdefinitions> <button x:name="part_previousbutton" width="28" height="20" grid.column="0" grid.row="0" horizontalalignment="left" focusable="false"> </button> <button x:name="part_headerbutton" grid.column="1" grid.row="0" horizontalalignment="center" verticalalignment="center" focusable="false" fontsize="10.5" fontweight="bold"> </button> <button x:name="part_nextbutton" width="28" height="20" grid.column="2" grid.row="0" horizontalalignment="right" focusable="false"> </button> <grid x:name="part_monthview" grid.columnspan="3" grid.row="1" horizontalalignment="center" margin="6,-1,6,6" visibility="visible"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="*"/> <rowdefinition height="*"/> <rowdefinition height="*"/> <rowdefinition height="*"/> <rowdefinition height="*"/> <rowdefinition height="*"/> <rowdefinition height="*"/> </grid.rowdefinitions> </grid> <grid x:name="part_yearview" grid.columnspan="3" grid.row="1" horizontalalignment="center" margin="6,-3,7,6" visibility="hidden"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> <columndefinition width="auto"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="auto"/> <rowdefinition height="auto"/> </grid.rowdefinitions> </grid> </grid> </border> </border> </grid> <controltemplate.triggers> <datatrigger binding="{binding displaymode, relativesource={relativesource findancestor, ancestorlevel=1, ancestortype={x:type calendar}}}" value="year"> <setter targetname="part_monthview" property="visibility" value="hidden"/> <setter targetname="part_yearview" property="visibility" value="visible"/> </datatrigger> <datatrigger binding="{binding displaymode, relativesource={relativesource findancestor, ancestorlevel=1, ancestortype={x:type calendar}}}" value="decade"> <setter targetname="part_monthview" property="visibility" value="hidden"/> <setter targetname="part_yearview" property="visibility" value="visible"/> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style> </calendar.calendaritemstyle> </calendar> </grid> </page>
this stacktrace (sorry system in japanese, normal nullreferenceexception). pasted xaml in brand new wpf application project. im not using automation or else.
system.nullreferenceexception はハンドルされませんでした。 message=オブジェクト参照がオブジェクト インスタンスに設定されていません。 source=presentationcore stacktrace: 場所 system.windows.automation.peers.automationpeer.ensurechildren() 場所 system.windows.automation.peers.automationpeer.updatechildreninternal(int32 invalidatelimit) 場所 system.windows.automation.peers.automationpeer.updatechildren() 場所 system.windows.automation.peers.automationpeer.updatesubtree() 場所 system.windows.automation.peers.automationpeer.updatesubtree() 場所 system.windows.contextlayoutmanager.fireautomationevents() 場所 system.windows.contextlayoutmanager.updatelayout() 場所 system.windows.uielement.updatelayout() 場所 system.windows.interop.hwndsource.process_wm_size(uielement rootuielement, intptr hwnd, windowmessage msg, intptr wparam, intptr lparam) 場所 system.windows.interop.hwndsource.layoutfiltermessage(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) 場所 ms.win32.hwndwrapper.wndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) 場所 ms.win32.hwndsubclass.dispatchercallbackoperation(object o) 場所 system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) 場所 ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) 場所 system.windows.threading.dispatcher.invokeimpl(dispatcherpriority priority, timespan timeout, delegate method, object args, int32 numargs) 場所 ms.win32.hwndsubclass.subclasswndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam) 場所 ms.win32.unsafenativemethods.showwindow(handleref hwnd, int32 ncmdshow) 場所 system.windows.window.showhelper(object booleanbox) 場所 system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) 場所 ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) 場所 system.windows.threading.dispatcheroperation.invokeimpl() 場所 system.windows.threading.dispatcheroperation.invokeinsecuritycontext(object state) 場所 system.threading.executioncontext.runtrycode(object userdata) 場所 system.runtime.compilerservices.runtimehelpers.executecodewithguaranteedcleanup(trycode code, cleanupcode backoutcode, object userdata) 場所 system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state) 場所 system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean ignoresyncctx) 場所 system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) 場所 system.windows.threading.dispatcheroperation.invoke() 場所 system.windows.threading.dispatcher.processqueue() 場所 system.windows.threading.dispatcher.wndprochook(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) 場所 ms.win32.hwndwrapper.wndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam, boolean& handled) 場所 ms.win32.hwndsubclass.dispatchercallbackoperation(object o) 場所 system.windows.threading.exceptionwrapper.internalrealcall(delegate callback, object args, int32 numargs) 場所 ms.internal.threading.exceptionfilterhelper.trycatchwhen(object source, delegate method, object args, int32 numargs, delegate catchhandler) 場所 system.windows.threading.dispatcher.invokeimpl(dispatcherpriority priority, timespan timeout, delegate method, object args, int32 numargs) 場所 ms.win32.hwndsubclass.subclasswndproc(intptr hwnd, int32 msg, intptr wparam, intptr lparam) 場所 ms.win32.unsafenativemethods.dispatchmessage(msg& msg) 場所 system.windows.threading.dispatcher.pushframeimpl(dispatcherframe frame) 場所 system.windows.threading.dispatcher.pushframe(dispatcherframe frame) 場所 system.windows.threading.dispatcher.run() 場所 system.windows.application.rundispatcher(object ignore) 場所 system.windows.application.runinternal(window window) 場所 system.windows.application.run(window window) 場所 system.windows.application.run() innerexception:
i had similar problem , following post helped me: http://social.msdn.microsoft.com/forums/ar/wpf/thread/0ee9954d-0df5-4d61-8dc9-eb50c7a5be99.
in case change:
x:key="{componentresourcekey resourceid=daytitletemplate, typeintargetassembly={x:type calendaritem}}"
to
x:key="{x:static calendaritem.daytitletemplateresourcekey}"
Comments
Post a Comment