c# - WPF User Control Resources -
i have custom control extending wpf datagrid. inside custom control, resources , control template of grid defined
<datagrid.resources> <local:visibilityconverter x:key="visibilityconverter" /> <style x:key="columnheadergripperstyle" targettype="{x:type thumb}"> <setter property="width" value="8" /> <setter property="background" value="transparent" /> <setter property="cursor" value="sizewe" /> <setter property="template"> <setter.value> <controltemplate targettype="{x:type thumb}"> <border padding="{templatebinding padding}" background="{templatebinding background}" /> </controltemplate> </setter.value> </setter> </style> <!--may need commented if clashes other styles--> <style targettype="{x:type datagridcolumnheader}" x:key="datagridcolumnheaderstyle"> <setter property="foreground" value="#ff000000" /> <setter property="horizontalcontentalignment" value="left" /> <setter property="verticalcontentalignment" value="center" /> <setter property="istabstop" value="false" /> <setter property="separatorbrush" value="#ffc9caca" /> <setter property="padding" value="4" /> <setter property="template"> <setter.value> <controltemplate targettype="datagridcolumnheader"> <grid x:name="root" height="48"> <grid.rowdefinitions> <rowdefinition /> <rowdefinition height="26" /> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition /> <columndefinition width="auto" /> </grid.columndefinitions> <visualstatemanager.visualstategroups> <visualstategroup x:name="commonstates"> <visualstate x:name="normal" /> <visualstate x:name="mouseover"> <storyboard> <coloranimation storyboard.targetname="backgroundrectangle" storyboard.targetproperty="(fill).color" duration="0" to="#ff448dca" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[3].color" duration="0" to="#7fffffff" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[2].color" duration="0" to="#ccffffff" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[1].color" duration="0" to="#f2ffffff" /> </storyboard> </visualstate> <visualstate x:name="pressed"> <storyboard> <coloranimation storyboard.targetname="backgroundrectangle" storyboard.targetproperty="(fill).color" duration="0" to="#ff448dca" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[0].color" duration="0" to="#d8ffffff" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[1].color" duration="0" to="#c6ffffff" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[2].color" duration="0" to="#8cffffff" /> <coloranimation storyboard.targetname="backgroundgradient" storyboard.targetproperty="(fill).(gradientstops)[3].color" duration="0" to="#3fffffff" /> </storyboard> </visualstate> </visualstategroup> <visualstategroup x:name="sortstates"> <visualstate x:name="unsorted" /> <visualstate x:name="sortascending"> <storyboard> <doubleanimation storyboard.targetname="sorticon" storyboard.targetproperty="opacity" duration="0" to="1.0" /> </storyboard> </visualstate> <visualstate x:name="sortdescending"> <storyboard> <doubleanimation storyboard.targetname="sorticon" storyboard.targetproperty="opacity" duration="0" to="1.0" /> <doubleanimation storyboard.targetname="sorticon" storyboard.targetproperty="(rendertransform).scaley" duration="0" to="-.9" /> </storyboard> </visualstate> </visualstategroup> </visualstatemanager.visualstategroups> <rectangle x:name="backgroundrectangle" grid.columnspan="2" fill="#ff1f3b53" stretch="fill" /> <rectangle x:name="backgroundgradient" grid.columnspan="2" stretch="fill"> <rectangle.fill> <lineargradientbrush endpoint=".7,1" startpoint=".7,0"> <gradientstop color="#fcffffff" offset="0.015" /> <gradientstop color="#f7ffffff" offset="0.375" /> <gradientstop color="#e5ffffff" offset="0.6" /> <gradientstop color="#d1ffffff" offset="1" /> </lineargradientbrush> </rectangle.fill> </rectangle> <grid grid.columnspan="2" margin="0,0,1,1"> <border margin="0,0,0,-1" width="14" borderbrush="#ff878787" borderthickness="1,0,0,0" horizontalalignment="right" visibility="collapsed"> <path x:name="dropdownindicator" height="5.26" margin="0,2,0,0" width="6.334" data="f1 m-3.0081699e-07,0.010437 l4.0493598,6.9732699 l8.0546999,0 l-3.0081699e-07,0.010437 z" fill="#ff444444" horizontalalignment="center" rendertransformorigin=".5,.5" stretch="uniform" uselayoutrounding="false" verticalalignment="center"> <path.rendertransform> <scaletransform scalex=".9" scaley=".9" /> </path.rendertransform> </path> </border> </grid> <grid margin="4,2,9,2" horizontalalignment="{templatebinding horizontalcontentalignment}" verticalalignment="{templatebinding verticalcontentalignment}"> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition width="auto" /> <columndefinition width="auto" /> </grid.columndefinitions> <local:columnoptioncontrol height="13" width="13" /> <!--visibility="{binding path=canusergroup, relativesource={relativesource ancestortype={x:type datagrid}}, converter={staticresource visibilityconverter}}" />--> <contentpresenter grid.column="1" content="{templatebinding content}" /> <path x:name="sorticon" grid.column="2" margin="4,0,0,0" width="8" data="f1 m -5.215,6.099l 5.215,6.099l 0,0l -5.215,6.099 z " fill="#ff444444" horizontalalignment="right" opacity="0" rendertransformorigin=".5,.5" stretch="uniform" verticalalignment="center"> <path.rendertransform> <scaletransform scalex=".9" scaley=".9" /> </path.rendertransform> </path> </grid> <rectangle x:name="verticalseparator" grid.column="1" width="1" fill="{templatebinding separatorbrush}" verticalalignment="stretch" /> <!--visibility="{templatebinding separatorvisibility}" />--> <border grid.columnspan="2" grid.row="1" margin="0" borderbrush="#ffc9caca" borderthickness="0,1,1,0" visibility="{binding path=canuserfilter, relativesource={relativesource ancestortype={x:type datagrid}}, converter={staticresource visibilityconverter}}"> <local:columnfiltercontrol margin="0,0,0,1"/> <!--filtervisibility="{binding path=canuserfilter, relativesource={relativesource ancestortype={x:type datagrid}}, converter={staticresource visibilityconverter}}" />--> </border> <thumb style="{staticresource columnheadergripperstyle}" x:name="part_leftheadergripper" horizontalalignment="left" /> <thumb style="{staticresource columnheadergripperstyle}" x:name="part_rightheadergripper" horizontalalignment="right" /> </grid> </controltemplate> </setter.value> </setter> </style> </datagrid.resources>
and visibilityconverter defined
[valueconversion(typeof(visibility), typeof(bool))] class visibilityconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { bool? val = value bool?; if (val == null) val = false; visibility visible = (val == false ? visibility.collapsed : visibility.visible); return visible; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { visibility? visible = value visibility?; if (visible == null) visible = visibility.collapsed; bool val = (visible == visibility.collapsed || visible == visibility.hidden ? false : true); return val; } }
in usage of control, when attempt declare resources below, run time exception thrown @ below declaration
'set property 'system.windows.resourcedictionary.deferrablecontent' threw exception.' line number '368' , line position '30'.
with inner exception
{"cannot re-initialize resourcedictionary instance."}
<local:enhancedwpfdatagrid.resources> <style targettype="{x:type datagridcell}"> <eventsetter event="previewmouseleftbuttondown" handler="datagridcell_previewmouseleftbuttondown"></eventsetter> </style> <uc:settings x:key="settings"/> </local:enhancedwpfdatagrid.resources>
if comment above resources xaml, works fine.
could please shed lights wrong this?
thanks in advance.
i think you're trying redefine resourcedictionary
you've created when setting custom control. you've created resource dictionary use control, , you're attempting create again (hence re-initialize exception).
you move styling datagridcell
main template (which wouldn't want in case, because of evensetter
, presumably instance specific).
a better solution may define style in resourcedictionary
, reference it, or add style
resources window/page
you're using custom control (e.g. add resources of parent grid
).
i believe can merge resource dictionaries, although i've never tried it: http://msdn.microsoft.com/en-us/library/aa350178.aspx
Comments
Post a Comment